spring - 使用 REST 根资源类作为接口(interface),得到 "no operation matching request"

标签 spring rest tomcat7 cxf m2eclipse

问题:根资源类定义为具有所有注释的接口(interface)。 CXFServlet 无法在 impl 类上看到 POST 操作,尽管它是在接口(interface)上定义的。 当所有注释都复制到 impl 类中时,它工作正常。

注意:仅在接口(interface)上定义时,GET 工作正常,只有 POST 导致问题。

@Path("foo/")
public interface TestService {
    @Path("foo/{id}")
    @GET
    @Produces("text/plain")
    public String getIt(String id);

@Path("foo")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ElementClass(response = Bar.class)
public Response createStuff(@Context MessageContext context,
        Bar bar);

@Features(features = "org.apache.cxf.feature.LoggingFeature")  
public class TestServiceImpl implements TestService {
    @Override
    public String getIt(String id) {
        return "Hi there!";
    }
@Override
public Response createStuff(@Context MessageContext context,
        Bar bar) {
    bar.set...
    bar.set...
    return Response.ok(bar).build();

Beans.xml {

<jaxrs:server id="testService" address="/test">
        <jaxrs:serviceBeans>
            <ref bean="testservice1"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="testservice1" class="foo.bar.TestServiceImpl"/>

Web.xml

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    ……..
    </listener>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

从Tomcat7.0服务器打印的请求: 使用 Chrome postman

信息:入站消息

编号:1

Address: http://localhost:8080/<war-name>/test/foo/foo
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/json
Headers: {Accept=[*/*], accept-encoding=[gzip,deflate,sdch], accept-language=[en-US,en;q=0.8], cache-control=[no-cache], connection=[keep-alive], Content-Length=[144], content-type=[application/json], host=[localhost:8080], origin=[chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm], user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36]}
Payload: {
    "bar": {
        "create_time": "Fri Sep 20 17:51:40 PDT 2013",
        "update_time": "e0739141-1e8c-48ad-b8ad-410331b3dba3",
    }
}

错误: 2013 年 9 月 21 日下午 12:13:05 org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethod 警告:未找到匹配请求路径“//test/foo/foo”的操作,相对路径:/foo,HTTP 方法:POST,内容类型:application/json,接受:/,.请启用 FINE/TRACE 日志级别以获取更多详细信息。 2013 年 9 月 21 日下午 12:13:05 org.apache.cxf.interceptor.LoggingOutInterceptor

INFO: Outbound Message
---------------------------
ID: 1
Response-Code: 404
Content-Type: text/xml
Headers: {Allow=[GET, OPTIONS, HEAD], Date=[Sat, 21 Sep 2013 19:13:05 GMT], Content-Length=[0]}

最佳答案

我自己解决了这个问题。在 imll 类方法中,不要重新声明带注释的参数。

在 Impl 类中的这个片段中

@Override
public Response createStuff(*@Context* MessageContext context,
        Bar bar) {

我不必要地使用了 @Context 注释,这会导致它失效。一旦我从 impl 类中删除注释,它就可以正常工作。没错,当你在接口(interface)中指定它时,为什么要再次装饰方法参数。

关于spring - 使用 REST 根资源类作为接口(interface),得到 "no operation matching request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937144/

相关文章:

java - 配置 Spring Batch 而不保留元数据

java - Hibernate的SQLQueryexecuteUpdate函数启动,但没有完全执行

java - 启动后更改Spring Cloud Config服务器uri

Java - JSON 输出并不像控制台所建议的那样

delphi - 如何使用http更新datasnap Rest服务器中的文件?

java - 使用 tomcat 7 容器配置 Weld 2.0 项目时出错

html - web-app 上下文和 FORM ACTION 属性?

java - Spring库中的REST(RestTemplate)是否支持HTTPS协议(protocol)?

rest - REST API 和 Web 客户端的服务器配置

java - Tomcat启动报错如何解决?