当前位置:   article > 正文

springboot实现webservice接口自定义返回值通过postman测试_springboot webservice postman 测试

springboot webservice postman 测试

震惊~~都2023年了竟然还有人用webservice!

1、springboot集成webservice

maven添加依赖

  1. <!--通过cxf实现webservice-->
  2.         <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web-services</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.cxf</groupId>
  8. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  9. <version>3.5.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.cxf</groupId>
  13. <artifactId>cxf-rt-transports-http</artifactId>
  14. <version>3.5.1</version>
  15. </dependency>
  16.         <!--解析xml-->
  17. <dependency>
  18. <groupId>org.dom4j</groupId>
  19. <artifactId>dom4j</artifactId>
  20. <version>2.1.3</version>
  21. </dependency>

添加配置文件

  1. @Configuration
  2. public class CxfConfig {
  3.     //自定义拦截器 用于处理返回数据格式
  4. @Autowired
  5. private CxfOutterceptor cxfOutterceptor;
  6. @Bean
  7. public ServletRegistrationBean disServlet() {
  8. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");
  9. return servletRegistrationBean;
  10. }
  11. @Bean(name = Bus.DEFAULT_BUS_ID)
  12. public SpringBus springBus() {
  13. return new SpringBus();
  14. }
  15. @Bean
  16. public Endpoint endpoint() {
  17. EndpointImpl endpoint = new EndpointImpl(springBus(), new AccountServiceImpl());
  18. //发布一个名为AccountService的webservice服务,可以多个
  19.         endpoint.publish("/AccountService");
  20. endpoint.getOutInterceptors().add(cxfOutterceptor);
  21. return endpoint;
  22. }
  23. }

自定义拦截器

  1. @Component
  2. public class CxfOutterceptor extends AbstractPhaseInterceptor<Message> {
  3. public CxfOutterceptor() {
  4. super(Phase.PRE_STREAM);
  5. }
  6. public CxfOutterceptor(String phase) {
  7. super(phase);
  8. }
  9. @Override
  10. public void handleMessage(Message message) throws Fault {
  11. try {
  12. OutputStream os = message.getContent(OutputStream.class);
  13. CachedStream cs = new CachedStream();
  14. message.setContent(OutputStream.class, cs);
  15. message.getInterceptorChain().doIntercept(message);
  16. CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
  17. InputStream in = csnew.getInputStream();
  18. String xml = IOUtils.toString(in);
  19. StringBuffer stringBuffer = new StringBuffer();
  20. //去掉外层包装
  21. Document document = DocumentHelper.parseText(xml);
  22. Element rootElement = document.getRootElement();
  23. List<Element> elements1 = rootElement.elements();
  24. if(elements1!=null && elements1.size()>0){
  25. Element element = elements1.get(0);
  26. List<Element> elements = element.elements();
  27. if(elements!=null && elements.size()>0){
  28. Element aReturn = elements.get(0).element("return");
  29. String name = aReturn.getName();
  30. String adata = aReturn.getData().toString();
  31. stringBuffer = new StringBuffer(adata);
  32. }
  33. }
  34. //这里对xml做处理,处理完后同理,写回流中
  35. IOUtils.copy(new ByteArrayInputStream(stringBuffer.toString().getBytes()), os);
  36. cs.close();
  37. os.flush();
  38. message.setContent(OutputStream.class, os);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. private class CachedStream extends CachedOutputStream {
  44. public CachedStream() {
  45. super();
  46. }
  47. protected void doFlush() {
  48. //currentStream.flush();
  49. }
  50. protected void doClose(){
  51. }
  52. protected void onWrite(){
  53. }
  54. }
  55. }

通过cxf实现wenservice服务返回结果是有一层固定包装的,类似下图,return标签里才是结果,如何完全自定义返回的结果数据呢?就需要上面的拦截器去掉外层的包装。

service类

  1. @WebService(name = "AccountService",
  2. targetNamespace = "http://server.webservice.kaka.com"
  3. )
  4. public interface AccountService {
  5. @WebMethod
  6. public String sendMessage(@WebParam(name = "username",targetNamespace="http://server.webservice.kaka.com") String username);
  7. @WebMethod
  8. public boolean getFlag(@WebParam(name = "username",targetNamespace="http://server.webservice.kaka.com") String username);
  9. @WebMethod
  10. public String queryUsers();
  11. }

实现类

  1. @WebService(serviceName = "AccountService",
  2.         //名命空间可任意写 要跟service里的保持一致
  3. targetNamespace = "http://server.webservice.kaka.com",
  4.         //接口类的全路径
  5. endpointInterface = "com.kaka.webService.AccountService"
  6. )
  7. @Component
  8. @Slf4j
  9. public class AccountServiceImpl implements AccountService {
  10. @Override
  11. public String sendMessage(String username) {
  12. return "=====Hello! " + username + "=====";
  13. }
  14. @Override
  15. public boolean getFlag(String username) {
  16. return false;
  17. }
  18. }

接下来启动服务,浏览器访问ip:端口/webservice/AccountService?wsdl

即可看到wsdl信息

2、postman测试

Headers添加

key:Content-type

value:text/xml;charset=utf-8

然后Body选择raw,XML

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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/">
  3. <soap:Body>
  4. <sendMessage xmlns="http://server.kaka.com">
  5. <username>测试啊</username>
  6.     </sendMessage>
  7. </soap:Body>
  8. </soap:Envelope>

如果使用了上面的自定义拦截器,则调用的返回结果为:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/133062
推荐阅读
相关标签
  

闽ICP备14008679号