springboot中基于JAX-WS发布webService,soap1.1和soap1.2的问题

一、场景说明:

        对接海康8200,接受海康卡口车辆数据,webService方式交互数据,海康为客户端使用协议soap1.2,我们服务端使用jax-ws进行的发布,默认使用的协议soap1.1。soap1.2客户端无法访问soap1.1的服务端。此时客户端进行请求,服务器端会报状态码415——后台程序不支持提交的content-type(协议不匹配 ),经过协调,我们服务端用soap1.2替换soap1.1,且发布的wsdl文件应符合海康给出的wsdl规范。

二、代码:

  1. interface接口:IHikThirdBayonetService
    import javax.jws.WebService;
    
    @WebService
    public interface IHikThirdBayonetService {
    
        public String initSystem( String xml);
    
        public String insertVehicleInfo ( String xml);
    
    }

     

  2. interface接口实现:HikThirdBayonetService(命名空间值根据实际需求定)
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.xml.ws.RequestWrapper;
    import javax.xml.ws.ResponseWrapper;
    
    @WebService
    public class HikThirdBayonetService implements IHikThirdBayonetService{
    
        @WebMethod(action = "urn:initSystem")
        @RequestWrapper(localName = "initSystem", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        @ResponseWrapper(localName = "initSystemResponse", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        @WebResult(name = "return", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        public String initSystem(@WebParam(name = "xml", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")String xml) {
            System.out.println("===="+xml);
            StringBuilder sb = new StringBuilder();
            sb.append("1failure,username or password is not correct.");
            return sb.toString();
        }
    
        @WebMethod(action = "urn:insertVehicleInfo")
        @RequestWrapper(localName = "insertVehicleInfo", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        @ResponseWrapper(localName = "insertVehicleInfoResponse", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        @WebResult(name = "return", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")
        public String insertVehicleInfo(@WebParam(name = "xml", targetNamespace = "http://service.thirdBayonet.webservice.bms.hikvision.com")String xml) {
            System.out.println("+++++++"+xml);
            StringBuilder sb = new StringBuilder();
            sb.append("-1InsertVehicleInfo failure");
            return sb.toString();
        }
    }

     

  3. webService服务发布(找个main方法能启动就可以发布,springboot的启动刚好通过main方法,发布的entpoint写在里面就可以)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.xml.ws.Endpoint;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//springboot的启动
		SpringApplication.run(DemoApplication.class, args);

		//webService发布的wsdl
		String address = "http://localhost:5300/services/ThirdBayonetService";

		//webService应用的实体类
		HikThirdBayonetService hik = new HikThirdBayonetService();

		//第一种发布:最简单直接的webService发布,默认使用的是soap1.1
		//Endpoint.publish(address,hik);

		//第二种发布:使用的是soap1.2,可能会出现报错--Cannot generate WSDL for binding ,原因jar包版本低
		//Endpoint.create("http://www.w3.org/2003/05/soap/bindings/HTTP/",hik).publish(address);

		//第三种发布:使用的是xsoap1.2,可以处理客户端soap1.2,会出现警告--为指定的绑定生成非标准 WSDL
		Endpoint.create("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/",hik).publish(address);
	}
}

三、小点

             <1>soap1.1协议产生的wsdl文件如下:

             springboot中基于JAX-WS发布webService,soap1.1和soap1.2的问题_第1张图片

             <2>soap1.2协议产生的wsdl文件如下:

             springboot中基于JAX-WS发布webService,soap1.1和soap1.2的问题_第2张图片

             注:只要服务端生成的wsdl有标签,则服务端便可接收soap1.2的客户端请求。

  1. jax-ws发布webService,不同soap协议会产生不同的wsdl文本。
  2. 每行中的url都是对应的bindingId,对应不同的协议。
    X_SOAP12_HTTP = new BindingID.SOAPHTTPImpl(SOAPVersion.SOAP_12, "http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/", true);
    SOAP12_HTTP = new BindingID.SOAPHTTPImpl(SOAPVersion.SOAP_12, "http://www.w3.org/2003/05/soap/bindings/HTTP/", true);
    SOAP11_HTTP = new BindingID.SOAPHTTPImpl(SOAPVersion.SOAP_11, "http://schemas.xmlsoap.org/wsdl/soap/http", true);
    SOAP12_HTTP_MTOM = new BindingID.SOAPHTTPImpl(SOAPVersion.SOAP_12, "http://www.w3.org/2003/05/soap/bindings/HTTP/?mtom=true", true, true);
    SOAP11_HTTP_MTOM = new BindingID.SOAPHTTPImpl(SOAPVersion.SOAP_11, "http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true", true, true);
    XML_HTTP = new BindingID.Impl(SOAPVersion.SOAP_11, "http://www.w3.org/2004/08/wsdl/http", false) 
    REST_HTTP = new BindingID.Impl(SOAPVersion.SOAP_11, "http://jax-ws.dev.java.net/rest", true)
  3. @WebMethod、@RequestWrapper、@responseWrapper、@WebResult、@WebParam这些注解用在interface中,在main方法中创建发布时候  创建服务实体对象 interface xx = new interfaceImpl(),在本地没有问题,但是在生产环境中就是不行,后来都改为注解到impl实现类中,如上面代码没有问题,这个实际上就是通过实现类而不是interface进行的webService发布。 
  4. 请求头信息的区别:soap1.1:Content-Type: text/xml ;soap1.2:Content-Type: application/soap+xml 
  5. @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING) ,jax-ws发布webService,在要发布的interface或者interfaceImpl上使用该标签如果不生效,可以试试
    @BindingType(''http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"),如果还是不生效,那就用上面create那种方式试试,理论上都是可以生效的,但因为jar包版本,开发环境等等多种原因有的好使,有的不好使,以生产环境为主,根据实际情况来试验。
  6. 不同环境选择不同的soap协议进行发布,可能遇到的报错:Cannot generate WSDL for binding  ...,可能遇到的警告:
    为制定的绑定生成非标准 WSDL,解决方式:使用xsoap1.2服务端来处理soap1.2客户端请求,对于警告可以直接忽视或者用比较新的jar并且用soap1.2服务端来处理soap1.2客户端请求。

你可能感兴趣的:(springboot中基于JAX-WS发布webService,soap1.1和soap1.2的问题)