随笔 - 434,  文章 - 0,  评论 - 463,  阅读 - 46万

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

步骤 1 搭建项目

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

image

步骤 2 配置pom.xml

<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

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

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

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

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>
posted on   剽悍一小兔  阅读(27)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示