Monday, October 14, 2019

JAX-RS read JSON POST body parameters

 Set a Response Body in JAX-RS

https://www.baeldung.com/jax-rs-response

 

 Read JSON directly


    @POST
    @Path("listwrapper/")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    public String getListViaWrapperHTTPRequest(List<String> list) {

        System.out.println("getListViaWrapperHTTPRequest");

        for(String param: list){
            System.out.println("param=" + param);
        }
        return "SOME_RESPONSE";
    }




Read HTTP request body


    @POST
    @Path("listraw/")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    public String getListViaRawHTTPRequest(@Context HttpServletRequest request, byte[] bodyBytes) {

        System.out.println("getListViaRawHTTPRequest");

        System.out.println("Content-Type: " + request.getContentType().toString());

        String body = new String(bodyBytes);

        System.out.println("body=" + body);

       return "SOME_RESPONSE";

    }

No comments:

Post a Comment