POST JSON in body request with Jersey

后端 未结 1 1863
滥情空心
滥情空心 2021-01-15 01:40

I have a dynamic web project in Java (deployed on a local application server Tomcat 7) which uses Jersey for creating REST APIs.

I do not use any build automation to

相关标签:
1条回答
  • 2021-01-15 01:52

    You should tell Jersey to use Gson for JSON handling. You can do this implementing the MessageBodyReader and MessageBodyWriter interfaces provided by Jersey. In this stackoverflow question you can find an example (see the accepted answer). If you want to add some context to it, take a look at the JSON section of the Jersey 1.x manual

    EDIT : I may not be understanding your code, but where is the call to Gson in your UserBeanMessageBodyReader? Try chaging the reaedFrom method with something along the lines of

    String result = new BufferedReader(new InputStreamReader(entityStream))
                     .lines().collect(Collectors.joining("\n"));
    Gson gson = new GSon();
    User myUser = gson.fromJson(result, User.class);
    

    instead of using JAXBContext (which afaict understands only XML inputs).

    0 讨论(0)
提交回复
热议问题