开发者社区> 问答> 正文

使用jax-rs调用resteasy服务使报错?报错

使用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服务;

展开
收起
爱吃鱼的程序员 2020-06-12 14:49:15 785 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    当我把参数改为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
                .target(REST_SERVICE_URL)
                .request()
                .post(Entity.entity(book,MediaType.APPLICATION_XML)).readEntity(String.class);
    2020-06-12 14:49:31
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载