How to receive json object in the RESTful (WEB SERVICE) POST method in Java

后端 未结 1 968
[愿得一人]
[愿得一人] 2021-01-22 04:09

It may be bit silly question, but I am confuse if it is possible to receive the JSON Objects in the @FormParam in the POST method under RESTful webservice

@POST         


        
相关标签:
1条回答
  • 2021-01-22 04:25

    This is possible however you need to understand what is @FormParam, basically it receive the values from an specific html form, as you probably know @FormParam need to know what is the param you want to take from the request using @FormParam("myParam"), if you want to consume json you need to provide it. So answer is you dont need to use @FormParam to consume JSON

    The above means instead of send a pair key/value properties you need to send the full json, obviously you need to generate that json using probably jquery or even javascript then sent that to your endpoint that should be something like.

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/fetch/")
    public List<MyObject> getCandidate(MyBean mybean){}
    

    Where MyBean class need to have the fields that are going to be sent within the JSON. The magic here is thanks to the MessageBodyReader, you should have jackson in your classpath, you can find an example here. Also will be good idea if you read this.

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