WebService

x33g5p2x  于2022-08-17 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(899)

WebService

依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web-services</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.cxf</groupId>
  7. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  8. <version>3.1.6</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.cxf</groupId>
  12. <artifactId>cxf-rt-transports-http</artifactId>
  13. <version>3.1.6</version>
  14. </dependency>

服务端

创建接口

  1. package com.primeton.mq.service;
  2. import javax.jws.WebService;
  3. @WebService(name = "DemoService", // 暴露服务名称
  4. targetNamespace = "http://service.mq.primeton.com"// 命名空间,一般是接口的包名倒序
  5. )
  6. public interface DemoService {
  7. public String sayHello(String user);
  8. }

创建接口实现类

  1. package com.primeton.mq.service.impl;
  2. import com.primeton.mq.service.DemoService;
  3. import javax.jws.WebService;
  4. import java.util.Date;
  5. @WebService(serviceName = "DemoService", // 与接口中指定的name一致
  6. targetNamespace = "http://service.mq.primeton.com", // 与接口中的命名空间一致,一般是接口的包名倒
  7. endpointInterface = "com.primeton.mq.service.DemoService"// 接口地址
  8. )
  9. public class DemoServiceImpl implements DemoService {
  10. @Override
  11. public String sayHello(String user) {
  12. return user+",现在时间:"+"("+new Date()+")";
  13. }
  14. }

创建CXF的配置类

  1. package com.primeton.mq.webServiceConfig;
  2. import com.primeton.mq.service.DemoService;
  3. import com.primeton.mq.service.impl.DemoServiceImpl;
  4. import org.apache.cxf.Bus;
  5. import org.apache.cxf.bus.spring.SpringBus;
  6. import org.apache.cxf.jaxws.EndpointImpl;
  7. import org.apache.cxf.transport.servlet.CXFServlet;
  8. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import javax.xml.ws.Endpoint;
  12. @Configuration //1
  13. public class CxfConfig {
  14. @Bean
  15. public ServletRegistrationBean cxrfServlet() { //2
  16. return new ServletRegistrationBean(new CXFServlet(),"/demo/*"); //3
  17. }
  18. @Bean(name = Bus.DEFAULT_BUS_ID)
  19. public SpringBus springBus() {
  20. return new SpringBus();
  21. }
  22. @Bean
  23. public DemoService demoService() { //4
  24. return new DemoServiceImpl(); //5
  25. }
  26. @Bean
  27. public Endpoint endpoint() {
  28. EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
  29. endpoint.publish("/api");//6
  30. return endpoint;
  31. }
  32. }

1:注意不要忘记configuration注解
2:注意命名,不要命名成dispatcherServlet,不然会报错。
3:我觉得就固定写法嘛,反正请求时类似http://localhost:8089/webservice/userService?wsdl。
4:就是你要开放的接口的接口类型。
5:就是你要开放的接口的接口类型的实现类。
6:就是你想要将你的接口暴露出来之后,别人使用时的名字,自己拟定一个。

启动服务

访问:http://localhost:8090/demo/api?wsdl

创建客户端

  1. package com.swagger.demo.controller;
  2. import com.swagger.demo.model.entity.Code;
  3. import com.swagger.demo.service.CodeService;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.util.List;
  12. /**
  13. * UserController类
  14. *
  15. * <p>
  16. * <b>History:</b>
  17. * <table border="1">
  18. * <tr>
  19. * <th>Date</th>
  20. * <th>Operator</th>
  21. * <th>Memo</th>
  22. * </tr>
  23. * <tr>
  24. * <td>2021/8/25 17:51</td>
  25. * <td>zrc</td>
  26. * <td>Create</td>
  27. * </tr>
  28. * </table>
  29. *
  30. * @author zrc
  31. * @version 1.0.0
  32. * @since 1.0.0
  33. */
  34. @Api(tags = "热点数据接口")
  35. @RestController
  36. @RequestMapping("codeController")
  37. public class CodeController {
  38. @Autowired
  39. private CodeService codeService;
  40. /**
  41. * 静态变量:系统日志
  42. */
  43. private static final Log logger = LogFactory.getLog(CodeController.class);
  44. @ApiOperation(value = "测试webservice")
  45. @PostMapping("/testWebservice")
  46. public List<Code> testWebservice() throws InterruptedException {
  47. // 接口地址
  48. String address = "http://localhost:8089/webservice/userService?wsdl";
  49. // 代理工厂
  50. JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
  51. // 设置代理地址
  52. jaxWsProxyFactoryBean.setAddress(address);
  53. // 设置接口类型
  54. jaxWsProxyFactoryBean.setServiceClass(CodeService.class);
  55. // 创建一个代理接口实现
  56. codeService = (CodeService) jaxWsProxyFactoryBean.create();
  57. // 调用代理接口的方法调用并返回结果
  58. List<Code> codes = codeService.getCodes();
  59. System.out.println("返回结果: " + codes);
  60. return codes;
  61. }
  62. }

Restful风格的webservice SpringBoot与JAX-RS

依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.projectlombok</groupId>
  7. <artifactId>lombok</artifactId>
  8. <optional>true</optional>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.cxf</groupId>
  12. <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
  13. <version>3.4.1</version>
  14. </dependency>
  15. <!--json转换-->
  16. <dependency>
  17. <groupId>org.apache.cxf</groupId>
  18. <artifactId>cxf-rt-rs-extension-providers</artifactId>
  19. <version>3.4.1</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.codehaus.jettison</groupId>
  23. <artifactId>jettison</artifactId>
  24. <version>1.4.1</version>
  25. </dependency>

service

  1. @WebService
  2. public interface UserService {
  3. @POST
  4. void saveUser(User user);
  5. @PUT
  6. void updateUser(User user);
  7. @DELETE
  8. @Path("/{id}")
  9. void deleteUser(@PathParam("id") Integer id);
  10. @GET
  11. List<User> findAllUser();
  12. @GET
  13. @Path("/{id}")
  14. //转json
  15. @Produces(MediaType.APPLICATION_JSON)
  16. User findById(@PathParam("id")Integer id);
  17. }

实现类跟ws的一样

配置类

  1. @Configuration
  2. public class JaxRsConfig {
  3. @Autowired
  4. private Bus bus;
  5. @Autowired
  6. private UserService userService;
  7. @Bean
  8. public Server createdServer(){
  9. JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
  10. //设置访问地址
  11. endpoint.setAddress("/userService");
  12. //设置bus
  13. endpoint.setBus(bus);
  14. //设置实体类对象
  15. endpoint.setServiceBean(userService);
  16. return endpoint.create();
  17. }
  18. }

浏览器访问,如果要别的格式的响应,就到服务端接口上改 @Produces(MediaType.APPLICATION_JSON),自行查看MediaType

相关文章