简体   繁体   English

使用REST根资源类作为接口,获取“无操作匹配请求”

[英]Using REST root resource class as interface, getting “no operation matching request”

Problem: Root resource class defined as interface with all annotations. 问题:根资源类定义为具有所有注释的接口。 CXFServlet is not able to see the POST operation on the impl class, though it's defined on the interface. CXFServlet无法在impl类上看到POST操作,尽管它是在接口上定义的。 When all the annotations are copied into the impl class, it works fine. 当所有注释都复制到impl类中时,它可以正常工作。

Note: The GET works fine when only defined on the interface, only the POST is causing the problem. 注意:仅在接口上定义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 { 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 在web.xml

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

Reuest printed from Tomcat7.0 server: Using Chrome Postman 从Tomcat7.0服务器打印的Reuest:使用Chrome Postman

INFO: Inbound Message 信息:入站消息

ID: 1 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",
    }
}

Error: Sep 21, 2013 12:13:05 PM org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethod WARNING: No operation matching request path "//test/foo/foo" is found, Relative Path: /foo, HTTP Method: POST, ContentType: application/json, Accept: / ,. 错误: 2013年9月21日下午12:13:05 org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethod警告: 找不到匹配请求路径的操作“// test / foo / foo”,相对路径:/ foo,HTTP方法:POST,ContentType:application / json,Accept: / ,. Please enable FINE/TRACE log level for more details. 请启用FINE / TRACE日志级别以获取更多详细信息。 Sep 21, 2013 12:13:05 PM org.apache.cxf.interceptor.LoggingOutInterceptor 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]}

I figured out the problem myself. 我自己想出了问题。 In the imll class methods, don't re-declare the params with annotations. 在imll类方法中,不要使用注释重新声明params。

In this snippet inside the Impl class 在Impl类中的这个片段中

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

I used the @Context annotation unnecessarily, which is throwing it off. 我不必要地使用了@Context注释,这就是抛弃它。 Once I remove the annotation from the impl class, it works just fine. 一旦我从impl类中删除了注释,它就可以正常工作。 And rightly so, why to decorate the method params again when you specified it in the interface. 这是正确的,为什么在界面中指定它时再次装饰方法params。

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

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