Java WebService可以很容易地集成到Spring框架中。以下是一些簡單的步驟:
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>3.0.10.RELEASE</version>
</dependency>
@Configuration
@EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("ServicePort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://example.com");
wsdl11Definition.setSchema(schema);
return wsdl11Definition;
}
@Bean
public XsdSchema schema() {
return new SimpleXsdSchema(new ClassPathResource("schema.xsd"));
}
}
@Endpoint
注解標記:@Endpoint
public class MyWebService {
private static final String NAMESPACE_URI = "http://example.com";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "request")
@ResponsePayload
public JAXBElement<Response> handleRequest(@RequestPayload JAXBElement<Request> request) {
//處理請求并返回響應
}
}
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
PayloadLoggingInterceptor interceptor = new PayloadLoggingInterceptor();
interceptors.add(interceptor);
}
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
}
通過以上步驟,你可以很容易地將Java WebService集成到Spring框架中,實現SOAP服務的開發和部署。