1. JDK自带工具生成代码
1.1 代码
-s表示生成Java源代码
wsimport -s xx?wsdl
2. CXF
2.1 下载安装
下载地址: http://archive.apache.org/dist/cxf/
环境配置:
2.2 生成客户端代码
2.3 可能出现的问题
SpringBoot集成WebService
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starterjaxws</artifactId>
<version>3.3.6</version>
</dependency>
使用cxf-spring-boot-starterjaxws 3.3.6的版本springboot版本为2.2.5可以启动,2.3以上不行
- 服务端
webservice接口
@WebService(targetNamespace = "http://service.demo.example.com")
public interface UserService {
@WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
public User getUser(@WebParam(name = "userId") String userId);
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public String getUserName(@WebParam(name = "userId") String userId);
实现类
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.demo.example.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.example.webservice.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Service
public class UserServiceImpl implements UserService {
private Map<String, User> userMap = new HashMap<String, User>();
public UserServiceImpl() {
System.out.println("向实体类插入数据");
User user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test1");
user.setEmail("Jerry@163.xom");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test2");
user.setEmail("Jerryfix@163.xom");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test3");
user.setEmail("Jerryfix@163.xom");
userMap.put(user.getUserId(), user);
}
@Override
public User getUser(String userId) {
System.out.println("userMap是:"+userMap);
return userMap.get(userId);
}
@Override
public String getUserName(String userId) {
return "userId为:" + userId;
}
cxfconfig
springboot高版本已经cxf结合了,这个bean不需要写,可以直接配置yml
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
/**
* 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
* 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl springboot高版本已经cxf结合了,这个bean不需要写,可以直接配置yml
* @return
*/
/*@SuppressWarnings("all")
@Bean
public ServletRegistrationBean disServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}*/
/** JAX-WS
* 站点服务
* **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, userService);
endpoint.publish("/user");
return endpoint;
}
}
cxf:
#此时wsdl地址为 localhost:8080/service/user?wsdl
path: /service
- 客户端