【问题标题】:Using REST root resource class as interface, getting "no operation matching request"使用REST根资源类作为接口,得到“无操作匹配请求”
【发布时间】:2013-09-21 20:15:38
【问题描述】:

问题: 根资源类定义为具有所有注释的接口。 CXFServlet 无法看到 impl 类的 POST 操作,尽管它是在接口上定义的。 将所有注解复制到 impl 类中后,就可以正常工作了。

注意: 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 邮递员

信息:入站消息

ID: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 WARNING: 没有找到匹配请求路径“//test/foo/foo”的操作, 相对路径:/foo,HTTP 方法:POST,ContentType:application/json,Accept:/,。请启用 FINE/TRACE 日志级别以获取更多详细信息。 2013 年 9 月 21 日 12:13:05 PM 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]}

【问题讨论】:

  • 您究竟是如何发送响应的?你能把@Produces(MediaType.APPLICATION_JSON)改成@Produces("text/plain")

标签: spring rest tomcat7 cxf m2eclipse


【解决方案1】:

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

在这个sn-p里面的Impl类

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-25
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多