一、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>
二、写一个接口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"表示发布出去的方法名字是get_name,不写则使用默认getName(驼峰化之后)
@WebParam接口方法的参数
mode = Mode.IN表示输入的参数,非必填
name = "user_id"表示参数的名字是user_id,不写则使用默认arg0,arg1
@WebResult(name = "result")接口方法返回结果名字为result
三、接口的实现类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
四、发布服务
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/*");// 发布服务名称
}
@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());// 绑定要发布的服务
endpoint.publish("/user"); // 显示要发布的名称
return endpoint;
}
}
启动后访问http://localhost:8080/service/user?wsdl
网友评论