I am using Jersey Rest Service. I am getting clients requests in json and getting java object out of it. Everything works fine. However, is there anyway I can get the exact json that was pass from client without even converting to java object.
Issue is json request contents just two parameters and below ObjectMapper converts back to Json but with null values. To ignore, I have to put #JsonInclude(Include.NON_NULL) on each pojo class. If I can get just client json, then it would be good.
ObjectMapper mapper=new ObjectMapper();
String jsonString= mapper.writeValueAsString(body);
Keep the following in your resource method:
#Consumes("application/json")
public Response fn(String payload){} //Notice the payload of type string.
That way the payload will be just a String. I am still not sure why you want it this way, but it should work.
Related
We're using Jersey with a Jackson object mapper to parse JSON input. So, we're doing this:
#POST
#Path("/placeOrder")
#Consumes("application/json")
#Produces({MediaType.APPLICATION_JSON})
public Response placeOrder(OrderDetails orderDetails) {
where OrderDetails is a parsed representation of the JSON body (parsed by the Jackson parser). It works well, but we'd like to be able to log the input request body for debugging purposes. It seems like I can do that with the LoggingFeature property:
rc.register(new LoggingFeature(java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME),
java.util.logging.Level.INFO, LoggingFeature.Verbosity.PAYLOAD_TEXT, 10000));
But that logs way too much information, including headers and the whole response. I'd like to capture or log just the unparsed JSON request body, but I can't find a way to get at it in Jersey.
I'm using a Micronaut controller which receives a list of objects as a payload
#Post
Mono<HttpResponse<?>> ingest(#Body final List<MyObject> payload) {
[...]
}
Some clients are sending a list of 'MyObject' containing objects which cannot be deserialized by Jackson
Is there an easy way to split the payload in deserializable and non-deserializable objects?
We would need to keep the non-deserializable for further analysis
Fixing the client sending the payload is unfortunately not an option at the moment
I tried to play around with the Micronaut error handling : https://docs.micronaut.io/2.0.0.M2/guide/index.html#errorHandling, but didn't manage to find an easy way to do what I need
We need to bulid up a json payload to send to a rest endpoint. Our org uses Jackson. The only way to build up the json (without creating dozens of nested empty pojos) is as follows:
ObjectMapper mapper = new ObjectMapper();
ObjectNode rootNode = mapper.createObjectNode();
ObjectNode content = mapper.createObjectNode();
rootNode.set("someContent", content);
content.put("somekey","someval");
... lots more nested objects created here...
Ok, so now I have a complex json object in the mapper. How do I get it out?
E.g. How do I get a json string out of it suitable for sending to an REST api as post payload?
There are various examples of setting up pretty print, but these revolve around jsonizing a single java object (which I am not doing), or outputting to streams or files, not a simple String.
Any suggestions?
perhaps use ObjectMapper.writeValueAsString:
System.out.println(mapper.writeValueAsString(rootNode));
I am facing an issue when i am sending a REST request in JSON format.Some of the parameters are getting missed when it invokes the service.However it works fine when i send the request in xml format.The issue which i am geting throws below error:
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "create_session_param"
The object mapper class looks as below:
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);
JaxbAnnotationModule module = new JaxbAnnotationModule();
// maintain JAXB annotation support
objectMapper.registerModule(module);
Can someone please help me to resolve this?
Thanks.
You only have WRAP_ROOT_VALUE which is for serialization. Remember serialization is POJO to JSON. The SerializationFeature.WRAP_ROOT_VALUE is the one that actually adds the "create_session_param" when creating the JSON.
We need JSON to POJO, which is deserialization, which has its own feature set. In this case, we need a feature to unwrap the root value in the JSON. For that there is
DeserializationFeature.UNWRAP_ROOT_VALUE
So do
mapper.configure(DeserializationFeature UNWRAP_ROOT_VALUE, true);
I am building project REST project with Spring Boot and i come across an issue of sending JSON from client to server.
My Scenerio is i want to send json like this using postman REST client:
{
"test":"success"
}
And want to get this json using this method:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public Map<String, Object> postData(#RequestBody Map map){
log.info("in test Connection method");
return map;
}
I am using above method but it is giving exception.
If it is not possible to process json data with #RequestBody with POST request then is there any other way to get json data with POST request and process that json data?
I have just tested it here and it works fine.
You have to specify the Content-Type header in your POST request though, and set it to application/json. You can easily do this in Postman in the Headers tab.`
Without it you will most likely get an Internal Server Error (500) saying
Content type 'text/plain;charset=UTF-8' not supported