springboot集成webService开发详解

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

webService优缺点

webService优点

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

webService缺点

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

springboot集成webService

环境准备

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

集成开发

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

-新建webservice接口

@Component
@WebService(name = "InvoiceWebService", targetNamespace = WsConst.INVOICE_NAMESPACE_URI)
public interface InvoiceWebService {

   /**
    * @param invData 参数
    * @return 返回处理数据
    */
   @WebMethod(action = WsConst.INVOICE_NAMESPACE_URI + "/PaperOutputInvoice", operationName = "PaperOutputInvoice")
   @WebResult(name = "PaperOutputInvoiceResult", targetNamespace = WsConst.INVOICE_NAMESPACE_URI)
   public String PaperOutputInvoice(@WebParam(name = "invData", targetNamespace = 
          WsConst.INVOICE_NAMESPACE_URI) String invData) throws ServiceException, MalformedURLException, RemoteException;
}
  • 接口实现
@Configuration
@WebService(
        // 与接口中指定的name一致
        serviceName = "InvoiceWebService",
        // 与接口中的命名空间一致,一般是接口的包名倒
        targetNamespace = WsConst.INVOICE_NAMESPACE_URI ,
        // 接口地址
        endpointInterface = "com.juneyaoair.soapserver.service.InvoiceWebService"
)
public class InvoiceWebServiceImpl implements InvoiceWebService {

    private static final Logger logger = LoggerFactory.getLogger(InvoiceWebServiceImpl.class);

    @Value("${sop.invoice.web.service.endpoint}")
    private String endpoint;

    @Override
    public String PaperOutputInvoice(@WebParam(name = "invData", targetNamespace = WsConst.INVOICE_NAMESPACE_URI) String invData) throws ServiceException, MalformedURLException, RemoteException {
        logger.info("入参参数invData==========>:" + invData); 
    }
}
  • 编写配置类发布接口
@Configuration
public class WebConfig {

   @Autowired
   private Bus bus;
   @Autowired
   InvoiceWebService invoiceWebService;
   /**
    * 接口发布
    * @return 返回
    */
   @Bean
   public Endpoint invoiceEndpoint() {
       EndpointImpl endpoint = new EndpointImpl(bus, invoiceWebService);
       System.out.println("---------------------------接口发布开始---------------------------------------");
       endpoint.publish("/InvoiceWebService");
       System.out.println("---------------------------接口发布成功---------------------------------------");
       return endpoint;
   }
}
  • 启动项目接口发布成功,可以访问接口
    http://localhost:8000/services

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

  • 可以使用SoapUI进行测试

测试(java代码测试)

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

相关文章

微信公众号

最新文章

更多