问题:根资源类定义为具有所有注释的接口。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();
}
豆类.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>
Reuest从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,ContentType:application/json,Accept:/,。请启用 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]}