繁体   English   中英

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

[英]Bind generated REST service interface implementation to servlet

我有一个wadl文件,可以使用wadl2java工具从其中生成REST服务的对象和接口。 我有这些接口的实现,到目前为止一切都很好。 我还在web.xml配置了servlet ,但是我无法使它正常工作。 现在,我的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...
    }
}

我认为这看起来不错,但是当我启动Web服务时,出现以下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'

看起来接口和实现之间的绑定是正确的,但是我不太明白为什么会收到此错误? 是的,为@Consumes@Produces批注定义了相同的MIME-types@Consumes ,但是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. 公开了2种POST方法,均侦听路径“ / services ”。
  2. 这两种方法都使用和产生相同的MimeType

问题 -如果该资源被击中。 然后,Jersey无法理解要获取结果要调用哪个API。 是getBar()还是getFoo()

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

@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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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