使用jax-ws和spring创建和web容器共端口的webservice服务
2013-01-28 17:03
429 查看
本文介绍的webservice是建立在与spring集成的条件下【jdk自带的webservices会占用一个独立的端口,通过这个方法配置不需要】
1、修改WEB.XML文件,在spring的监听器配置之后加上如下内容
这里创建了2个webservices,访问路径是http://xxx:xxx/xxx/getNameWS?wsdl 和 http://xxx:xxx/xxx/getCountWS?wsdl
2、编写webservice实现类
3、spring配置文件里添加如下内容
需要添加的jar包有:gmbal-api-only.jar/jaxb-impl.jar/jaxws-api.jar/jaxws-rt.jar/jaxws-spring-1.8.jar/management-api.jar/policy.jar/stax-ex.jar/streambuffer.jar/xbean-spring-3.0.jar
spring 的官方文档里有写,跟上面的方法有点差别:
Our
In this scenario, the endpoint instances are defined and managed as Spring beans themselves; they will be registered with the JAX-WS engine but their lifecycle will be up to the Spring application context. This means that Spring functionality like explicit dependency injection may be applied to the endpoint instances. Of course, annotation-driven injection through
The
The difference to the standard style of exporting servlet-based endpoints is that the lifecycle of the endpoint instances themselves will be managed by Spring here, and that there will be only one JAX-WS servlet defined in
Check out https://jax-ws-commons.dev.java.net/spring/ for the details on setup and usage style.
Where
Accessing the web service is now very easy as we have a bean factory for it that will expose it as
From the client code we can access the web service just as if it was a normal class:
NOTE: The above is slightly simplified in that JAX-WS requires endpoint interfaces and implementation classes to be annotated with
1、修改WEB.XML文件,在spring的监听器配置之后加上如下内容
<servlet> <servlet-name>JAXWSServlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSSpringServlet </servlet-class> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAXWSServlet</servlet-name> <url-pattern>/getNameWS</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JAXWSServlet</servlet-name> <url-pattern>/getCountWS</url-pattern> </servlet-mapping>
这里创建了2个webservices,访问路径是http://xxx:xxx/xxx/getNameWS?wsdl 和 http://xxx:xxx/xxx/getCountWS?wsdl
2、编写webservice实现类
@WebService @Component public class GetNameWSImpl{ @Resource private XXDao xxDao; @WebMethod public XXVO searchName(String id) throws Exception { XXVO returnVO try { returnVO = xxDao.xxx(id); } catch (Exception e) { e.printStackTrace(); throw new Exception("webservice内部异常"); } return returnVO; } }
3、spring配置文件里添加如下内容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd"> <wss:binding url="/getNameWS"> <wss:service> <ws:service bean="#getNameWSImpl" /> <!--注入实现类--> </wss:service> </wss:binding> </beans>
需要添加的jar包有:gmbal-api-only.jar/jaxb-impl.jar/jaxws-api.jar/jaxws-rt.jar/jaxws-spring-1.8.jar/management-api.jar/policy.jar/stax-ex.jar/streambuffer.jar/xbean-spring-3.0.jar
spring 的官方文档里有写,跟上面的方法有点差别:
21.5.5 Exposing servlet-based web services using JAX-WS
Spring provides a convenient base class for JAX-WS servlet endpoint implementations -SpringBeanAutowiringSupport. To expose our
AccountServicewe extend Spring's
SpringBeanAutowiringSupportclass and implement our business logic here, usually delegating the call to the business layer. We'll simply use Spring 2.5's
@Autowiredannotation for expressing such dependencies on Spring-managed beans.
/** * JAX-WS compliant AccountService implementation that simply delegates * to the AccountService implementation in the root web application context. * * This wrapper class is necessary because JAX-WS requires working with dedicated * endpoint classes. If an existing service needs to be exported, a wrapper that * extends SpringBeanAutowiringSupport for simple Spring bean autowiring (through * the @Autowired annotation) is the simplest JAX-WS compliant way. * * This is the class registered with the server-side JAX-WS implementation. * In the case of a Java EE 5 server, this would simply be defined as a servlet * in web.xml, with the server detecting that this is a JAX-WS endpoint and reacting * accordingly. The servlet name usually needs to match the specified WS service name. * * The web service engine manages the lifecycle of instances of this class. * Spring bean references will just be wired in here. */ import org.springframework.web.context.support.SpringBeanAutowiringSupport; @WebService(serviceName="AccountService") public class AccountServiceEndpoint extends SpringBeanAutowiringSupport { @Autowired private AccountService biz; @WebMethod public void insertAccount(Account acc) { biz.insertAccount(acc); } @WebMethod public Account[] getAccounts(String name) { return biz.getAccounts(name); } }
Our
AccountServletEndpointneeds to run in the same web application as the Spring context to allow for access to Spring's facilities. This is the case by default in Java EE 5 environments, using the standard contract for JAX-WS servlet endpoint deployment. See Java EE 5 web service tutorials for details.
21.5.6 Exporting standalone web services using JAX-WS
The built-in JAX-WS provider that comes with Sun's JDK 1.6 supports exposure of web services using the built-in HTTP server that's included in JDK 1.6 as well. Spring'sSimpleJaxWsServiceExporterdetects all
@WebServiceannotated beans in the Spring application context, exporting them through the default JAX-WS server (the JDK 1.6 HTTP server).
In this scenario, the endpoint instances are defined and managed as Spring beans themselves; they will be registered with the JAX-WS engine but their lifecycle will be up to the Spring application context. This means that Spring functionality like explicit dependency injection may be applied to the endpoint instances. Of course, annotation-driven injection through
@Autowiredwill work as well.
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"> <property name="baseAddress" value="http://localhost:8080/"/> </bean> <bean id="accountServiceEndpoint" class="example.AccountServiceEndpoint"> ... </bean> ...
The
AccountServiceEndpointmay derive from Spring's
SpringBeanAutowiringSupportbut doesn't have to since the endpoint is a fully Spring-managed bean here. This means that the endpoint implementation may look like as follows, without any superclass declared - and Spring's
@Autowiredconfiguration annotation still being honored:
@WebService(serviceName="AccountService") public class AccountServiceEndpoint { @Autowired private AccountService biz; @WebMethod public void insertAccount(Account acc) { biz.insertAccount(acc); } @WebMethod public List<Account> getAccounts(String name) { return biz.getAccounts(name); } }
21.5.7 Exporting web services using the JAX-WS RI's Spring support
Sun's JAX-WS RI, developed as part of the GlassFish project, ships Spring support as part of its JAX-WS Commons project. This allows for defining JAX-WS endpoints as Spring-managed beans, similar to the standalone mode discussed in the previous section - but this time in a Servlet environment. Note that this is not portable in a Java EE 5 environment; it is mainly intended for non-EE environments such as Tomcat, embedding the JAX-WS RI as part of the web application.The difference to the standard style of exporting servlet-based endpoints is that the lifecycle of the endpoint instances themselves will be managed by Spring here, and that there will be only one JAX-WS servlet defined in
web.xml. With the standard Java EE 5 style (as illustrated above), you'll have one servlet definition per service endpoint, with each endpoint typically delegating to Spring beans (through the use of
@Autowired, as shown above).
Check out https://jax-ws-commons.dev.java.net/spring/ for the details on setup and usage style.
21.5.8 Accessing web services using JAX-WS
Analogous to the JAX-RPC support, Spring provides two factory beans to create JAX-WS web service proxies, namelyLocalJaxWsServiceFactoryBeanand
JaxWsPortProxyFactoryBean. The former can only return a JAX-WS service class for us to work with. The latter is the full-fledged version that can return a proxy that implements our business service interface. In this example we use the latter to create a proxy for the
AccountServiceendpoint (again):
<bean id="accountWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"> <property name="serviceInterface" value="example.AccountService"/> <property name="wsdlDocumentUrl" value="http://localhost:8888/AccountServiceEndpoint?WSDL"/> <property name="namespaceUri" value="http://example/"/> <property name="serviceName" value="AccountService"/> <property name="portName" value="AccountServiceEndpointPort"/> </bean>
Where
serviceInterfaceis our business interface the clients will use.
wsdlDocumentUrlis the URL for the WSDL file. Spring needs this a startup time to create the JAX-WS Service.
namespaceUricorresponds to the targetNamespace in the .wsdl file.
serviceNamecorresponds to the service name in the .wsdl file.
portNamecorresponds to the port name in the .wsdl file.
Accessing the web service is now very easy as we have a bean factory for it that will expose it as
AccountServiceinterface. We can wire this up in Spring:
<bean id="client" class="example.AccountClientImpl"> ... <property name="service" ref="accountWebService"/> </bean>
From the client code we can access the web service just as if it was a normal class:
public class AccountClientImpl { private AccountService service; public void setService(AccountService service) { this.service = service; } public void foo() { service.insertAccount(...); } }
NOTE: The above is slightly simplified in that JAX-WS requires endpoint interfaces and implementation classes to be annotated with
@WebService,
@SOAPBindingetc annotations. This means that you cannot (easily) use plain Java interfaces and implementation classes as JAX-WS endpoint artifacts; you need to annotate them accordingly first. Check the JAX-WS documentation for details on those requirements.
相关文章推荐
- 使用jax-ws和spring创建和web容器共端口的webservice服务
- 使用JAX-WS创建webservice服务,含服务端及客户端
- WebService-01-使用JAX-WS发布WebService服务
- JAX-WS(一)之使用wsgen从Java创建简单的WebService
- Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针
- JAX-WS(一)之使用wsgen从Java创建WebService
- JAX-WS(二)之使用wsimport创建WebService客户端
- JAX-WS(一)之使用wsgen从Java创建WebService
- 利用myeclipse8.5 将使用spring配置的原工程用JAX-WS发布为webservice
- JAX-WS(二)之使用wsimport创建WebService客户端
- 使用CXF(JaxWsServerFactoryBean、ServerFactoryBean)编码的方式发布WebService服务
- JAX-WS(二)之使用wsimport创建WebService客户端
- JAX-WS使用wsgen和wsimport从Java创建WebService
- JAX-WS框架-Myeclipse创建客户端调用webservice服务
- 一、使用cxf的JaxWsServerFactoryBean创建webservice的服务端和客户端
- JAX-WS(二)之使用wsimport创建WebService客户端
- 【webservice】JAX-WS独立服务使用
- JAX-WS(二)之使用wsimport创建WebService客户端
- Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针
- webservice使用JDK自带的jax-ws创建并发布一个简单的webservice