简体   繁体   中英

Combined JAX-RS and JAX-WS

是否存在将JAX-RS和JAX-WS(或等效功能)组合到一个组合服务中的框架,库或技术,类似于在WCF中为同一服务使用两个端点(一个SOAP和一个REST)?

Apache CXF can do the job. Read more at http://cxf.apache.org/docs/frontends.html

It's possible with a standard tomcat configuration. Just use separate URLs for the services. I decided to put the JAX-WS service behind "SOAP/" and the others behind lowercase letters. If you want to use "rest" in the URL, it's even more easy, but not looking that nice for end users. Don't forget to add a sun-jaxws.xml, too. I left the `init-params as they are useful for normalized URLs. You can drop all of them if you wish.

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="webapp"
    version="2.5">
    <display-name>displayname</display-name>

    <filter>
        <filter-name>rest</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>thepackage</param-value>
        </init-param>
        <init-param>
            <!-- enables processing by JSPs if not JAX-RS handler is registered -->
            <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.CanonicalizeURIPath</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.NormalizeURI</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Redirect</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>rest</filter-name>
        <url-pattern>/firstresource/</url-pattern>
        <url-pattern>/secondresource/</url-pattern>

    </filter-mapping>

    <listener>
        <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>soap</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>soap</servlet-name>
        <url-pattern>/SOAP</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>

</web-app>

Addon to Mikhail's answer, example of CXF's configuration. More info is at http://cxf.apache.org/docs/jax-rs-and-jax-ws.html#JAX-RSandJAX-WS-JAXRSandJAXWS

  <!-- JAX-RS -->
  <jaxrs:server id="customerService" address="/">
    <jaxrs:serviceBeans>
      <ref bean="customerService" />
    </jaxrs:serviceBeans>
  </jaxrs:server>

  <!-- JAX-WS -->
  <jaxws:endpoint implementor="#customerService"
    address="/CustomerWorld" wsdlLocation="..."/>

  <bean id="customerService" class="demo.jaxrs.server.CustomerService" />

Update: Peter Szanto created a maven project at https://github.com/ExampleDriven/cxf-example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM