一涯冠、pom.xml里添加依賴↓
<!-- cxf支持 -->
<dependency>
????<groupId>org.apache.cxf</groupId>
????<artifactId>cxf-rt-frontend-jaxws</artifactId>
????<version>3.2.7</version>
</dependency>
<dependency>
????????<groupId>org.apache.cxf</groupId>
????????<artifactId>cxf-rt-transports-http</artifactId>
????????<version>3.2.7</version>
</dependency>
二它掂、寫一個(gè)接口UserService↓
package com.robot.transfer.webService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface UserService {
????@WebMethod(operationName = "get_name")
? ? @WebResult(name = "result")
????public String get_name(@WebParam(mode = Mode.IN, name = "user_id") String userId);
????@WebMethod(operationName = "get_user")
? ? @WebResult(name = "result")
????String getUser(String userId);
}
@WebMethod是接口方法注釋:
operationName = "get_name"表示發(fā)布出去的方法名字是get_name,不寫則使用默認(rèn)getName(駝峰化之后)
@WebParam接口方法的參數(shù)
mode = Mode.IN表示輸入的參數(shù),非必填
name = "user_id"表示參數(shù)的名字是user_id详囤,不寫則使用默認(rèn)arg0,arg1
@WebResult(name = "result")接口方法返回結(jié)果名字為result
三暴拄、接口的實(shí)現(xiàn)類UserServiceImpl↓
package com.robot.transfer.webService.impl;
import javax.jws.WebService;
import com.robot.transfer.webService.UserService;
@WebService(targetNamespace = "http://webService.transfer.robot.com/", endpointInterface = "com.robot.transfer.webService.UserService")
public class UserServiceImpl implements UserService {
????@Override
????public String getName(String userId) {
????????return "name:zhangqian";
????}
????@Override
????public String getUser(String userId) {
????????return "userId:" + userId + ";name:zhangqian";
????}
}
注意:targetNamespace和endpointInterface指向的都是UserService
四、發(fā)布服務(wù)
package com.robot.transfer.config;
import javax.xml.ws.Endpoint;
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 com.robot.transfer.webService.UserService;
import com.robot.transfer.webService.impl.UserServiceImpl;
@Configuration
public class WebServiceConfig {
????@Bean
????public ServletRegistrationBean myCXFServlet() {
????????return new ServletRegistrationBean(new CXFServlet(), "/service/*");// 發(fā)布服務(wù)名稱
????}
????@Bean(name = Bus.DEFAULT_BUS_ID)
????public SpringBus springBus() {
????????return new SpringBus();
????}
????@Bean
????public UserService userService() {
????????return new UserServiceImpl();
????}
????@Bean
????public Endpoint endpoint() {
????????EndpointImpl endpoint = new EndpointImpl(springBus(), userService());// 綁定要發(fā)布的服務(wù)
????????endpoint.publish("/user"); // 顯示要發(fā)布的名稱
????????return endpoint;
????}
}
啟動(dòng)后訪問http://localhost:8080/service/user?wsdl