将生成的REST服务接口实现绑定到servlet

时间:2016-03-18 08:59:28

标签: java rest servlets jersey jersey-client

我有一个wadl文件,我使用REST工具为wadl2java服务生成对象和接口。我有这些接口的实现,到目前为止一切都很好。我还在servlet中配置了web.xml,但我无法让它工作。现在我的web xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>com.foo.ws.RestServiceImpl</param-value>               
        </init-param>    
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

生成的rest界面如下所示:

package com.foo.schemas;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.foo.schemas.BarRequest;
import com.foo.schemas.BarResponse;
import com.foo.schemas.FooRequest;
import com.foo.schemas.FooResponse;

@Path("services")
public interface RestService {

    @POST
    @Consumes("application/xml")
    @Produces("application/xml")
    BarResponse getBar(BarRequest barRequest);

    @POST
    @Consumes("application/xml")
    @Produces("application/xml")
    FooResponse getFoo(FooRequest fooRequest);
}

它的实施:

package com.foo.ws;

import org.glassfish.jersey.server.ResourceConfig;
import com.foo.RestService;
import com.foo.schemas.BarRequest;
import com.foo.schemas.BarResponse;
import com.foo.schemas.FooRequest;
import com.foo.schemas.FooResponse;

public class RestServiceImpl extends ResourceConfig implements RestService {
    public RestServiceImpl () {
        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(RestServiceImpl.class).to(RestService.class);
            }
        });
    }

    @Override
    BarResponse getBar(BarRequest barRequest) {
        // Implementation omitted...
    }

    @Override
    FooResponse getFoo(FooRequest FooRequest) {
        // Implementation omitted...
    }
}

我认为这看起来很好但是当我启动网络服务时,我会关注HTTP ERROR 503

Problem accessing /my-web-service/. Reason:

org.glassfish.jersey.server.model.ModelValidationException: Validation of    
the application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP 
method POST and input mime-types as defined by"@Consumes" and "@Produces" 
annotations at Java methods public com.foo.schemas.BarResponse 
com.foo.ws.RestServiceImpl.getBar(com.foo.schemas.BarRequest) and public 
com.foo.schemas.FooResponse 
com.foo.ws.RestServiceImpl.getFoo(com.foo.schemas.FooRequest) at matching 
regular expression /services. These two methods produces and consumes 
exactly the same mime-types and therefore their invocation as a resource 
methods will always fail.; 
source='org.glassfish.jersey.server.model.RuntimeResource@5690c2a8'

看起来接口和实现之间的绑定是正确的但我真的不明白为什么我会收到此错误?是的,MIME-types@Consumes注释的方法定义为相同的@Produces,但这是wadl2java生成界面的方式吗?我不知道如何解决这个问题,servlet或wadl2java工具是否有任何配置错误?我没有任何线索,任何对此的帮助都非常感激,因为我坚持使用这个。

编辑 - 添加了wadl文件

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xs="http://www.w3.org/2001/XMLSchema" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:foo="http://schemas.foo.com" 
         xsi:schemaLocation="http://wadl.dev.java.net/2009/02    http://www.w3.org/Submission/wadl/wadl.xsd" 
         xmlns="http://wadl.dev.java.net/2009/02">
<doc xml:lang="en">This wadl describes....</doc>
<grammars>
    <include href="foo.xsd"/>
</grammars>
<resources base="/foo">
    <resource path="services">
        <method id="getBar" name="POST">
            <doc xml:lang="en">Get all bars.</doc>
            <request>
                <representation mediaType="application/xml">
                    <param required="true" style="plain" id="barRequest" name="barRequest" type="foo:BarRequest"/>
                </representation>
            </request>
            <response status="200">
                <representation mediaType="application/xml">
                    <param required="true" style="plain" id="barResponse" name="barResponse" type="foo:BarResponse"/>
                </representation>
            </response>
        </method>
        <method id="getFoo" name="POST">
            <doc xml:lang="en">Get all foos.</doc>
            <request>
                <representation mediaType="application/xml">
                    <param required="true" style="plain" id="fooRequest" name="fooRequest" type="foo:FooRequest"/>
                </representation>
            </request>
            <response status="200">
                <representation mediaType="application/xml">
                    <param required="true" style="plain" id="fooResponse" name="fooResponse" type="foo:FooResponse"/>
                </representation>
            </response>
        </method>
    </resource>
</resources>

1 个答案:

答案 0 :(得分:1)

问题是w.r.t资源如何暴露。

  1. 有2个POST方法已曝光,均听取路径“ / services ”。
  2. 这两种方法都采用并生成相同的MimeType
  3. 问题 - 是否会触发此资源。然后Jersey不会理解为了获得结果而调用哪个API。 是getBar()还是getFoo()

    现在,更改取决于期望是什么(根据需求,何时需要调用API)。 您必须相应地修改WADL以确保每个资源都侦听唯一路径/ consume / produce类型。

    @Path("services")
    public interface RestService {
    
        @POST
        @Consumes("application/xml")
        @Produces("application/xml")
        BarResponse getBar(BarRequest barRequest);
    
        @POST
        @Consumes("application/xml")
        @Produces("application/xml")
        FooResponse getFoo(FooRequest fooRequest);
    }