gpt4 book ai didi

java - 将生成的 REST 服务接口(interface)实现绑定(bind)到 servlet

转载 作者:行者123 更新时间:2023-12-01 10:12:01 25 4
gpt4 key购买 nike

我有一个 wadl 文件,我使用 wadl2java 工具从该文件生成 REST 服务的对象和接口(interface)。我已经实现了这些接口(interface),到目前为止一切都很好。我还在 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>

生成的其余接口(interface)如下所示:

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'

看起来接口(interface)和实现之间的绑定(bind)是正确的,但我不太明白为什么会出现此错误?是的,对于 @Consumes@Produces 注释,这些方法都使用相同的 MIME-types 进行定义,但这就是 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);
}

关于java - 将生成的 REST 服务接口(interface)实现绑定(bind)到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com