引入webservice需要使用jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.7</version>
</dependency>
书写接口和实现类
接口类
package org.andot.webservice.demo.server;
import org.andot.webservice.demo.entity.Person;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace = "http://andot.org/webservice/demo/server")
public interface TestApiService {
@WebMethod
Person insertPersonInfo(@WebParam(name = "PERSON", targetNamespace = "http://andot.org/webservice/demo/server") String person);
}
接口实现类
package org.andot.webservice.demo.server.provider;
import com.alibaba.fastjson.JSONArray;
import org.andot.webservice.demo.entity.Person;
import org.andot.webservice.demo.server.TestApiService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.List;
@Component
@WebService(name = "testApiService",
targetNamespace = "http://andot.org/webservice/demo/server",
endpointInterface = "org.andot.webservice.demo.server.TestApiService",
portName = "10000")
public class TestApiServiceImpl implements TestApiService {
@Override
public Person insertPersonInfo(String person) {
List<Person> list = JSONArray.parseArray(person, Person.class);
//TODO 逻辑处理
return list.get(0);
}
}
实体类
package org.andot.webservice.demo.entity;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Person {
private Integer id;
private String name;
private String niceName;
private Integer age;
private Double height;
}
配置类
package org.andot.webservice.demo.start;
import org.andot.webservice.demo.server.TestApiService;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 TestWebService {
@Bean
public ServletRegistrationBean wsServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Autowired
private TestApiService testApiService;
@Autowired
@Qualifier(Bus.DEFAULT_BUS_ID)
private SpringBus bus;
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus, testApiService);
endpoint.publish("/testApiService");
return endpoint;
}
}
访问地址
http://localhost:8080/ws/testApiService?wsdl
返回XML
使用POSTman进行测试
地址输入 http://localhost:8080/ws/testApiService
请求方式 POST
请求头 Context-Type:text/html
请求文本:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<insertPersonInfo xmlns="http://andot.org/webservice/demo/server">
<PERSON>
[{"name":"你好"},{"name":"你好2"}]
</PERSON>
</insertPersonInfo>
</soap:Body>
</soap:Envelope>
详细说明:
(1)insertPersonInfo是方法名。
(2)PERSON是方法参数。