Java笔记-Spring Boot中Spring WS WS-Addressing中@Action实例
此筆記為利用Spring WS的WS-Addressing發送SOAP請求及接收其響應。
WS-Addressing(Web服務尋址):傳送Web服務端點的引用的數據結構,以及一套能夠在特定的消息上關聯尋址信息的消息尋址屬性。
?
服務端
@Endpoint:此注解告訴Spring注解這是個類有資格處理soap請求。
@Action:此注解映射特定的soap行為,比如返回一個簡單的POJO類。
package cn.it1995.server;import cn.it1995.GetTestRequest; import cn.it1995.GetTestResponse; import cn.it1995.MyTest; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; import org.springframework.ws.soap.addressing.server.annotation.Action;@Endpoint public class IT1995Endpoint {@Action("http://it1995.cn/getTestRequest")public @ResponsePayloadGetTestResponse getTest(@RequestPayload GetTestRequest request){GetTestResponse response = new GetTestResponse();MyTest myTest = new MyTest();myTest.setId(request.getId());myTest.setName("Hello World");response.setMyTest(myTest);return response;} }關于空的soapAction
在配置Spring ws中,默認情況下都會生成包含空的soapAction的WSDL。可以覆蓋這個映射,設置適當的soapAction屬性。
如:
DefaultWsdl11Definition.setSoapActions(soapActions)
方法
package cn.it1995.server;import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.ws.config.annotation.EnableWs; import org.springframework.ws.transport.http.MessageDispatcherServlet; import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; import org.springframework.xml.xsd.SimpleXsdSchema; import org.springframework.xml.xsd.XsdSchema;import java.util.Properties;@EnableWs @Configuration public class SoapServerConfig {@Beanpublic ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext){MessageDispatcherServlet servlet = new MessageDispatcherServlet();servlet.setApplicationContext(applicationContext);servlet.setTransformWsdlLocations(true);return new ServletRegistrationBean(servlet, "/ws/*");}@Bean(name = "it1995")public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema){DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();wsdl11Definition.setPortTypeName("IT1995Port");wsdl11Definition.setLocationUri("/ws");wsdl11Definition.setTransportUri("http://it1995.cn/webservice");wsdl11Definition.setSchema(schema);//為動態生成的wsdl添加soap actionProperties soapActions = new Properties();soapActions.setProperty("getTest", "http://it1995.cn/getTestRequest");wsdl11Definition.setSoapActions(soapActions);return wsdl11Definition;}@Beanpublic XsdSchema it1995Schema(){return new SimpleXsdSchema(new ClassPathResource("xsd/MyData.xsd"));} }最后是關于Spring Boot的啟動項
package cn.it1995.server;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class RunServer {public static void main(String[] args){SpringApplication.run(RunServer.class);} }?
Spring WS-Addressing客戶端
配置客戶端其中WS-Addressing開啟soap請求。通過擴展WebServiceGateWaySupport創建client。通過WebServiceTemplate發送soap請求。首先創建ActionCallback提供了action url。這個location注冊了server方法及WSDL。最后通過WebServiceTemplate發送請求及獲取Response。
package cn.it1995.client;import cn.it1995.GetTestRequest; import cn.it1995.GetTestResponse; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; import org.springframework.ws.soap.addressing.client.ActionCallback;import java.net.URI; import java.net.URISyntaxException;public class IT1995Client extends WebServiceGatewaySupport {public GetTestResponse getTest(int id) throws URISyntaxException {GetTestRequest request = new GetTestRequest();request.setId(id);ActionCallback callback = new ActionCallback(new URI("http://it1995.cn/getTestRequest"));return (GetTestResponse)getWebServiceTemplate().marshalSendAndReceive(request, callback);} }配置client端,創建一個marshaller發送請求及獲取響應,初始化客戶端。
package cn.it1995.client;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller;@Configuration public class SoapClientConfig {@Beanpublic Jaxb2Marshaller marshaller(){Jaxb2Marshaller marshaller = new Jaxb2Marshaller();marshaller.setContextPath("cn.it1995");return marshaller;}@Beanpublic IT1995Client it1995Client(Jaxb2Marshaller marshaller){IT1995Client client = new IT1995Client();client.setDefaultUri("http://localhost:8080/ws/it1995");client.setMarshaller(marshaller);client.setUnmarshaller(marshaller);return client;} }配置client端,創建一個marshaller發送請求及獲取響應,初始化客戶端。
package cn.it1995.client;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller;@Configuration public class SoapClientConfig {@Beanpublic Jaxb2Marshaller marshaller(){Jaxb2Marshaller marshaller = new Jaxb2Marshaller();marshaller.setContextPath("cn.it1995");return marshaller;}@Beanpublic IT1995Client it1995Client(Jaxb2Marshaller marshaller){IT1995Client client = new IT1995Client();client.setDefaultUri("http://localhost:8080/ws/it1995");client.setMarshaller(marshaller);client.setUnmarshaller(marshaller);return client;} }使用AnnotationConfigApplicationContext初始化客戶端,下面是個調用例子:
package cn.it1995.client;import cn.it1995.GetTestResponse; import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.net.URISyntaxException;public class RunClient {public static void main(String[] args) throws URISyntaxException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SoapClientConfig.class);IT1995Client client = context.getBean(IT1995Client.class);GetTestResponse response = client.getTest(1);System.out.println(response.getMyTest().getId());System.out.println(response.getMyTest().getName());} }這里客戶端wsdl如下:
源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/WebServiceAction
?
總結
以上是生活随笔為你收集整理的Java笔记-Spring Boot中Spring WS WS-Addressing中@Action实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino笔记-外部中断实验(震动传
- 下一篇: Spring Boot笔记-banner