Springboot + cxf集成web service接口
教程分享
>
Java教程
>
Spring
(1083)
2024-08-07 11:05:25
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的webservice的配置过程,仅供参考。
一、项目是基于springboot搭建的webservice接口,首先是cxf接口,需要的jar包
<!-- cxf支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.1</version>
</dependency>
二、创建实体类,内容是用户信息查询和记录
package com.yeyue.cxfws.model;
import java.io.Serializable;
/**
* @ClassName: User
* @Description: user实体对象
* @author: xiaoboLi
* @date: 2017/11/20 17:20
* @Copyright: 2017 All rights reserved.
*/
public class User implements Serializable {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
三、接下来创建接口类
package com.yeyue.cxfws.service;
import com.yeyue.cxfws.model.User;
import javax.jws.WebService;
/**
* @ClassName: UserService
* @Description: 接口
* @author: xiaoboLi
* @date: 2017/11/20 17:22
* @Copyright: 2017 All rights reserved.
*/
@WebService
public interface UserService {
User getUser(Long id);
}
四、创建接口实现类
package com.yeyue.cxfws.service.impl;
import com.yeyue.cxfws.model.User;
import com.yeyue.cxfws.service.UserService;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: UserServiceImpl
* @Description: 描述
* @author: xiaoboLi
* @date: 2017/11/20 17:23
* @Copyright: 2017 All rights reserved.
*/
@WebService(targetNamespace = "http://service.cxfws.yeyue.com/", endpointInterface = "com.yeyue.cxfws.service.UserService")
public class UserServiceImpl implements UserService {
Map<Long, User> userMap = new HashMap<>();
public UserServiceImpl() {
System.out.println("向实体类插入数据");
User user = new User();
user.setId(1L);
user.setUsername("test1");
user.setPassword("aaaaaa");
userMap.put(user.getId(), user);
user.setId(2L);
user.setUsername("test1");
user.setPassword("aaaaaa");
userMap.put(user.getId(), user);
user.setId(3L);
user.setUsername("test1");
user.setPassword("aaaaaa");
userMap.put(user.getId(), user);
}
@Override
public User getUser(Long id) {
return userMap.get(id);
}
}
注释说明:在发布服务之前,需要在这里对service实现类的配置进行说明如图部分:
http://service.cxfws.yeyue.com/这是我的业务类所在路径
com.yeyue.cxfws.service.UserService这是我的接口所在路径
如果不加上上述内容,发布的接口路径wsdl中的targetNamespace会是类默认的路径,并且客户端在进行调用的时候会报错:
No operation was found with the name {http://impl.service.demo.paybay.cn/}getUser.
那么原因就是:在CXF发布服务的时候,发布的是业务类(UserServiceImpl.java),那么默认的命名空间就会是业务类所在包(路径),而对外界暴露的则是接口类(UserService.java),那么对于客户端调用的时侯,需要按照接口类所在路径进行命名空间的定义。
所以在发布之前我们要在业务类(UserServiceImpl.java)上增加注解,指定命名空间,然后再进行发布,

五、接下来我就要对webservice服务进行发布:
package com.yeyue.cxfws.config;
import com.yeyue.cxfws.service.UserService;
import com.yeyue.cxfws.service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import javax.xml.ws.Endpoint;
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;
/**
* @ClassName: WebServiceConfig
* @Description: 发布webservice接口
* 访问地址:http://localhost:8080/test/user?wsdl
* @author: xiaoboLi
* @date: 2017/11/20 17:35
* @Copyright: 2017 All rights reserved.
*/
@Configuration
public class CxfWebServiceConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
//接口部署的前部分地址
return new ServletRegistrationBean(new CXFServlet(), "/test/*");
}
@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());
//访问的wsdl路径
endpoint.publish("/user");
return endpoint;
}
}
那么到这里呢,我们的所有的步骤基本完成了,启动spring boot 然后再浏览器中输入url:http://localhost:8080/webservice/test/user?wsdl
可以看到有相关的wsdl描述信息输出了,说明服务已经发布了。
六、调用服务
package com.yeyue.cxfws.test;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
* @ClassName: Client
* @Description: 描述
* @author: xiaoboLi
* @date: 2017/11/20 18:18
* @Copyright: 2017 All rights reserved.
*/
public class Client {
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client =dcf.createClient("http://localhost:8080/webservice/test/user?wsdl");
Object[] objects=client.invoke("getUser",1L);
System.out.println("*****"+objects[0].toString());
}
}
最后附上项目层级结构
https://www.leftso.com/article/314.html