Service
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService(targetNamespace="http://tempuri.org/") @SOAPBinding(style=Style.DOCUMENT) public interface MyService { @WebMethod String sayHiTo(String name); @WebResult(name="sayHelloResult") String sayHello(@WebParam(name="name")String name); }
Service Impl
import javax.annotation.Resource; import javax.jws.WebService; import javax.servlet.http.HttpServletRequest; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPMessageContext; @WebService(serviceName = "myService", targetNamespace = "http://tempuri.org/", endpointInterface = "cn.config.MyService") public class MyServiceImpl implements MyService { @Override public String sayHiTo(String name) { return name; } @Resource private WebServiceContext wsc; @Override public String sayHello(String name) { MessageContext ctx = wsc.getMessageContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(SOAPMessageContext.SERVLET_REQUEST); return request.getContentType(); } }
javaBean Configuration
import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter;
@Configuration public class WebServiceConfig { @Bean public SimpleJaxWsServiceExporter simpleJaxWsServiceExporter() { SimpleJaxWsServiceExporter sjaxWsServiceExporter = new SimpleJaxWsServiceExporter(); sjaxWsServiceExporter.setBaseAddress("http://localhost:8081/services/"); return sjaxWsServiceExporter; } @Bean public MyServiceImpl myServiceImpl() { return new MyServiceImpl(); } }
服务器:tomcat 端口号:8081
wsdl:http://localhost:8081/services/helloservice?wsdl
说明及注意
(1)、通过http://localhost:8081/services/myService?wsdl 访问webservice部署描述符
还有自动生成的xsd:http://localhost:8081/services/myService?xsd=1 。
(2)、@SOAPBinding(parameterStyle=ParameterStyle.WRAPPED)
必须添加,否则会报错;另外,如果发布的方法只有一个参数可以使用@SOAPBinding(parameterStyle=ParameterStyle.BARE)。
(3)、@WebService(serviceName = "myService") 服务名称与Spring配置的bean一致。
(4)、webservice的端口设置不要与服务器一样,这一点非常重要否则服务器应用与webservice服务冲突会产生HTTP404错误。