简体   繁体   中英

Jersey request body needs to be well formed

I have a requirement, where my consumer needs the request to have a well-formed request body.

I am using jersy cleint and below is the code snippet.

SomeRequestObject vSomeRequestObject = getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).readValue(messageBody,
                            SomeRequestObject.class);

return restClient.target(urlMap.get("uri")).path(urlMap.get("path")).request(MediaType.APPLICATION_JSON)
                .headers(vRequestHeaders).post(Entity.entity(vSomeRequestObject, MediaType.APPLICATION_JSON));
            
            

But somehow the request is still like {"someKey1":"SomeValue1","someKey2":"SomeValue2"}

it should be like below

{
    "someKey1": "SomeValue1",
    "someKey2": "SomeValue2"
}

Please suggest if there is any jackson annotation to do so.

You have to prettify your output. Jackson is able to do it with the writerWithDefaultPrettyPrinter() method. This example below could help :

ObjectMapper objectMapper = new ObjectMapper();
SomeRequestObject object = SomeRequestObject.create();
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
System.out.println(json);

This link can help you for more details : https://www.tutorialspoint.com/pretty-print-json-using-jackson-library-in-java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM