前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java小白翻身 - webservice教程2

Java小白翻身 - webservice教程2

作者头像
剽悍一小兔
发布2021-07-20 10:21:03
6950
发布2021-07-20 10:21:03
举报

来一个HelloWorld,SpringBoot发布WebService可简单啦。

步骤 1 搭建项目

请参照这个教程搭建一个SpringBoot项目,注意,项目名字换成webService

image

步骤 2 配置pom.xml

代码语言:javascript
复制
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.6</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.6</version>
</dependency>

加上这两个jar包。

步骤 3 建services服务包

image

步骤 4 登陆接口类

设置一个登陆接口类

image

代码语言:javascript
复制
package com.webservice.demo.services;
import javax.jws.WebService;
import java.util.Map;

@WebService(name = "LoginService",              // 暴露服务名称
        targetNamespace = "http://java18.cn"    // 命名空间
)
public interface LoginService {

    Map<String,Object> userLogin();

}

步骤 5 登陆接口实现类

image

image

代码语言:javascript
复制
package com.webservice.demo.services.impl;

import com.webservice.demo.services.LoginService;

import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService(serviceName = "LoginService", // 与接口中指定的name一致
        targetNamespace = "http://java18.cn", // 与接口中的命名空间一致
        endpointInterface = "com.webservice.demo.services.LoginService"// 接口地址
)
public class LoginServiceImpl implements LoginService {

    @Override
    public Map<String, Object> userLogin() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("errCode",00000);
        resultMap.put("errMsg",null);
        return resultMap;
    }
}

步骤 6 创建CXF配置类

image

代码语言:javascript
复制
package com.webservice.demo.config;

import com.webservice.demo.services.LoginService;
import com.webservice.demo.services.impl.LoginServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public LoginService loginService() {
        return new LoginServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), loginService());
        endpoint.publish("/api");
        return endpoint;
    }

}

步骤 7 Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决

现在直接启动会报错的。

解决方法如下

image

image

这个方法名字换一下就好了。

步骤 8 访问webservice

启动项目,访问http://localhost:8080/webservice/api

image

步骤 9 访问wsdl

http://localhost:8080/webservice/api?wsdl

代码语言:javascript
复制
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://java18.cn" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="LoginService" targetNamespace="http://java18.cn">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://java18.cn" elementFormDefault="unqualified" targetNamespace="http://java18.cn" version="1.0">
<xs:element name="userLogin" type="tns:userLogin"/>
<xs:element name="userLoginResponse" type="tns:userLoginResponse"/>
<xs:complexType name="userLogin">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="userLoginResponse">
<xs:sequence>
<xs:element name="_return">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="entry">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="key" type="xs:string"/>
<xs:element minOccurs="0" name="value" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="userLoginResponse">
<wsdl:part element="tns:userLoginResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="userLogin">
<wsdl:part element="tns:userLogin" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="LoginService">
<wsdl:operation name="userLogin">
<wsdl:input message="tns:userLogin" name="userLogin"> </wsdl:input>
<wsdl:output message="tns:userLoginResponse" name="userLoginResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LoginServiceSoapBinding" type="tns:LoginService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="userLogin">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="userLogin">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="userLoginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LoginService">
<wsdl:port binding="tns:LoginServiceSoapBinding" name="LoginServiceImplPort">
<soap:address location="http://localhost:8080/webservice/api"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤 1 搭建项目
  • 步骤 2 配置pom.xml
  • 步骤 3 建services服务包
  • 步骤 4 登陆接口类
  • 步骤 5 登陆接口实现类
  • 步骤 6 创建CXF配置类
  • 步骤 7 Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
  • 步骤 8 访问webservice
  • 步骤 9 访问wsdl
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档