springboot集成webService开发详解

x33g5p2x  于2022-02-22 转载在 Spring  
字(2.9k)|赞(0)|评价(0)|浏览(509)

webService优缺点

webService优点

  • WebService是一种跨编程语言和跨操作系统平台的远程调用技术
  • 远程调用技术:不用担心防火墙的问题

webService缺点

  • 服务端接口方为webservice则客户端也必须使用webservice,双方保持一致
  • 因为webservice使用xml传输数据,因此性能上不能满足高并发
  • 写法,使用上不够优化,使用也很少

springboot集成webService

环境准备

  • springboot2.x
  • maven 3.6
  • idea开发工具
  • jdk8
  • mysql

集成开发

  • 搭建springboot项目引入依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web-services</artifactId>
  4. </dependency>

-新建webservice接口

  1. @Component
  2. @WebService(name = "InvoiceWebService", targetNamespace = WsConst.INVOICE_NAMESPACE_URI)
  3. public interface InvoiceWebService {
  4. /**
  5. * @param invData 参数
  6. * @return 返回处理数据
  7. */
  8. @WebMethod(action = WsConst.INVOICE_NAMESPACE_URI + "/PaperOutputInvoice", operationName = "PaperOutputInvoice")
  9. @WebResult(name = "PaperOutputInvoiceResult", targetNamespace = WsConst.INVOICE_NAMESPACE_URI)
  10. public String PaperOutputInvoice(@WebParam(name = "invData", targetNamespace =
  11. WsConst.INVOICE_NAMESPACE_URI) String invData) throws ServiceException, MalformedURLException, RemoteException;
  12. }
  • 接口实现
  1. @Configuration
  2. @WebService(
  3. // 与接口中指定的name一致
  4. serviceName = "InvoiceWebService",
  5. // 与接口中的命名空间一致,一般是接口的包名倒
  6. targetNamespace = WsConst.INVOICE_NAMESPACE_URI ,
  7. // 接口地址
  8. endpointInterface = "com.juneyaoair.soapserver.service.InvoiceWebService"
  9. )
  10. public class InvoiceWebServiceImpl implements InvoiceWebService {
  11. private static final Logger logger = LoggerFactory.getLogger(InvoiceWebServiceImpl.class);
  12. @Value("${sop.invoice.web.service.endpoint}")
  13. private String endpoint;
  14. @Override
  15. public String PaperOutputInvoice(@WebParam(name = "invData", targetNamespace = WsConst.INVOICE_NAMESPACE_URI) String invData) throws ServiceException, MalformedURLException, RemoteException {
  16. logger.info("入参参数invData==========>:" + invData);
  17. }
  18. }
  • 编写配置类发布接口
  1. @Configuration
  2. public class WebConfig {
  3. @Autowired
  4. private Bus bus;
  5. @Autowired
  6. InvoiceWebService invoiceWebService;
  7. /**
  8. * 接口发布
  9. * @return 返回
  10. */
  11. @Bean
  12. public Endpoint invoiceEndpoint() {
  13. EndpointImpl endpoint = new EndpointImpl(bus, invoiceWebService);
  14. System.out.println("---------------------------接口发布开始---------------------------------------");
  15. endpoint.publish("/InvoiceWebService");
  16. System.out.println("---------------------------接口发布成功---------------------------------------");
  17. return endpoint;
  18. }
  19. }
  • 启动项目接口发布成功,可以访问接口
    http://localhost:8000/services

  • 点击上图蓝色部分查看wsdl服务描述文档

  • 可以使用SoapUI进行测试

测试(java代码测试)

  1. // 调用过程
  2. Service service = new Service();
  3. Call call = (Call) service.createCall();
  4. // 设置接口地址
  5. call.setTargetEndpointAddress(new URL(endpoint));
  6. // 设置方法名称
  7. call.setOperationName(new QName( "http://tempuri.org/","PaperOutputInvoice"));
  8. // 操作的参数
  9. call.addParameter(new QName("http://tempuri.org/","invData"), XMLType.XSD_STRING, ParameterMode.IN);
  10. // 设置返回类型
  11. call.setReturnType(XMLType.XSD_STRING);
  12. call.setUseSOAPAction(true);
  13. // 给方法传递参数,并且调用方法
  14. Object[] obj = new Object[] {"{\"InvInfoList\":"+json2 + "}"};
  15. String result = (String) call.invoke(obj);

相关文章