2014-10-11 11 views
5

Bir dropwizard hizmetine gönderilen json isteğinin içeriğini okuman gerek. İletinin kendisi, dropwizard tarafından, yöntemin (PaymentMessage nesnesi) girdisi olan açıklamalı obbject'e serileştirilir. Yöntemin bir giriş parametresi olarak HttpServletRequest ekledim. HttpServletRequest boş değil, ancak HttpServletRequest#getInputStream() yöntemi, boş olmayan bir boş akışını döndürür.Bir dropwizard hizmetine gönderilen bir isteğin gövdesini okuyun

bukle: 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"}'

kodu:

@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(); 
} 
+0

Gövde, dediğiniz gibi PaymentMessage ile eşlenmelidir. Jackson'ın sizin için eşlemesi gereken POJO'nun bir parçası olmayan istek nesnesinden ne çıkmaya çalışıyorsunuz? – th3morg

+0

Iletinin gövdesini dize biçiminde almaya ve POJO nesnesine serileştirmeye çalışıyordum çünkü 1. Gövde isteğinin md5 toplamını doğrulamak için kullanmak istedim 2. Girişin seri hale getirilmesini istedim. nesne çünkü nesneler kullanımı daha kolay :) –

cevap

5

Sen json dize olmalıdır String paymentMessage için PaymentMessage paymentMessage parametreyi değiştirebilirsiniz. Daha sonra herhangi bir onaylama olmayacak, aksi takdirde POJO doğrudan olacak.

+0

Evet, sonunda yaptığım şey diziyi kendim POJO'ya serpiştiriyor. –