使用jax-rs调用resteasy服务使报错如下:
有遇到过相同问题的吗?
org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "user" (Class com.dtd365.domain.User), not marked as ignorable
at [Source: org.apache.catalina.connector.CoyoteInputStream@1bdc246f; line: 1, column: 10] (through reference chain: com.dtd365.domain.User["user"])
at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:183)
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:89)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:112)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
@Controller
public class HelloWorldController {
private static String REST_SERVICE_URL = "http://localhost:8080/mplus-rest/book";
public static void main(String[] args) {
/*
* User user = new User(11, "11", "11", "11", "11", "11", 1);
* user.setCreateDate(new Date()); TestUser tUser=new
* TestUser(123,"chushiyun");
*/
Book book = new Book();
book.setId(123);
book.setPrice(1);
book.setPublished(new Date());
book.setTitle("jcodjfop");
Client client = ClientBuilder.newClient();
client
.target(REST_SERVICE_URL)
.request()
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
这是调用的代码片段;
@Component
@Path("/book")
public class BookRest {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response saveBook(final Book book) {
System.out.println(book);
Book b=new Book();
return Response.ok(b).build();
}
}
这是resteasy服务;
当我把参数改为String类型时,接收到的参数为:
{"book":{"id":123,"title":"jcodjfop","price":1,"published":"2015-08-12T16:35:33.993+08:00"}}
不知道为什么为把book添加到json对象里,从而导致以上错误
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
publicResponsesaveBook(Stringbook){
System.out.println(book);
Bookb=newBook();
returnResponse.ok(b).build();
}
当我把MediaType给我XML时服务端可以正常映射为Book对象,但是返回值只能是json或xml格式,无法映射为Book对象;
Clientclient=ClientBuilder.newClient();
client版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。