Read the body of the request sent to the dropwizard service

I need to read the contents of a json request sent to the dropwizard service. The message itself is serialized by the dropwizard into an annotated obbject, which is the input to the method ( PaymentMessage object). I added HttpServletRequest as an input parameter to the method. HttpServletRequest not null, but the HttpServletRequest#getInputStream() method returns an empty stream empty , not containing zero.

Curl: curl -i -X POST -H'Content-Type: application/json; charset=UTF-8' \ http://localhost:8080/NL/users/555855/payments -d '{"eventId":"110099110099","hznHouseholdId":"1234567_nl","ipAddress":"123.123.123.123","transactionId":"799ef666-e09c-8350-247b-c466997714ad","transactionDate":"2014-09-29T16:56:21Z","appName":"Flappy Bird"}' curl -i -X POST -H'Content-Type: application/json; charset=UTF-8' \ http://localhost:8080/NL/users/555855/payments -d '{"eventId":"110099110099","hznHouseholdId":"1234567_nl","ipAddress":"123.123.123.123","transactionId":"799ef666-e09c-8350-247b-c466997714ad","transactionDate":"2014-09-29T16:56:21Z","appName":"Flappy Bird"}'

The code:

 @POST @Path("/{countryCode}/users/{customerId}/payments") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response processPaymentAction( @Context final HttpServletRequest request, @Nonnull @PathParam("countryCode") final String countryCode, @Nonnull @PathParam("customerId") final String customerId, @Valid PaymentMessage paymentMessage) throws IOException, ServletException { LOG.debug("Request "+request.toString()); final ByteSource byteSource = new ByteSource() { @Override public InputStream openStream() throws IOException { return request.getInputStream(); } }; LOG.debug("charset "+request.getCharacterEncoding()); final String contents = byteSource.asCharSource(Charset.forName(request.getCharacterEncoding())).read(); LOG.debug("contents: "+contents); return Response.status(Response.Status.ACCEPTED).build(); } 
+5
source share
1 answer

You can change the PaymentMessage paymentMessage parameter to String paymentMessage , which should be a json string. Then there will be no verification, although you will not have a POJO either.

+5
source

Source: https://habr.com/ru/post/1204479/


All Articles