How to automatically parse String @RequestBody as json

30,794

If you're using Jackson as your JSON parser, you can simply declare your parameter with the type TextNode. This is the Jackson type representing JSON strings.

public String updateName(@PathVariable(MY_ID) String myId, @RequestBody TextNode name) {

You can then use its asText method to retrieve its text value.

Share:
30,794
riddy
Author by

riddy

Java & Web Developer SOreadytohelp

Updated on July 22, 2022

Comments

  • riddy
    riddy almost 2 years

    I have an endpoint which should read a string value as body.

    @RestController
    public class EndpointsController {
       @RequestMapping( method = RequestMethod.PUT, value = "api/{myId}/name", consumes= MediaType.APPLICATION_JSON )
       public String updateName( @PathVariable( MY_ID ) String myId, @RequestBody String name) {
    
         //will be: "new name"
         //instead of : newname
         return myId;
       }
    }
    

    My problem is, that client will call this with "new name" which is correct IMHO but the server reads this with the quotes, because it does not handle the string as a json object. How can I tell Jackson to parse the string as well (same way than it does with Pojos)?

  • riddy
    riddy over 8 years
    thats it, thanks a lot! I hoped for an easy solution without changing the api
  • baraka
    baraka over 7 years
    Our API is now broken. It doesn't make sense to modify all services as suggested. I will search for a global configuration solution and let you know.
  • Sotirios Delimanolis
    Sotirios Delimanolis over 7 years
    @baraka Please clarify how your comment relates to this answer. What API? How is it broken? What services? When/where did I suggest modifying all of them? What global configuration? I really don't understand your comment.
  • baraka
    baraka over 7 years
    @SotiriosDelimanolis I'm terribly sorry for not explaining - we had an API (RestController) that worked fine until a spring 1.4.1 boot upgrade we did. Since than a few services that has a string parameter are now suddenly getting the string value with double qoutes - ""SomeString"" - so the value is "SomeString". This obviously causes us troubles further in the code. I have to admit we failed to resolve this by now and have already implemented your solution. Thnx anyway.
  • Andrew Hummel
    Andrew Hummel almost 7 years
    @baraka I had a very similar situation when upgrading a spring app that required a java upgrade from 1.6 to 1.8 and spring 3.0.4.RELEASE to 3.1.0.RELEASE. Since then, any requests being made to my controller whose methods accept @RequestBody String as the param, arrive in URL encoded format. I now have to manually decode them. Sounds like you used the above mentioned solution, but did you ever figure out why that started happening? Just because of a version upgrade?
  • baraka
    baraka almost 7 years
    @Andrew long time has passed but we recall it was just the version upgrade. We had no time to track the specific change-set :( IMHO changing the behavior of an interface is the same as breaking the interface contract.
  • RoutesMaps.com
    RoutesMaps.com almost 6 years
    Thank you. How should I correctly represent "some_string" in JSON for TextNode? If I send "some_string" I get Error: Unsupported Media Type
  • RoutesMaps.com
    RoutesMaps.com almost 6 years
    I get it. I should use @Consumes(MediaType.APPLICATION_JSON) instead of @Consumes(MediaType.TEXT_PLAIN)
  • RoutesMaps.com
    RoutesMaps.com almost 6 years
    Swagger generates for TextNode unintelligible Example Value: { "nodeType": "ARRAY", "array": true, "null": true, "number": true, "boolean": true, "float": true, "short": true, "object": true, "double": true, "bigDecimal": true, "textual": true, "bigInteger": true, "int": true, "binary": true, "valueNode": true, "long": true, "pojo": true, "missingNode": true, "integralNumber": true, "containerNode": true, "floatingPointNumber": true } So it looks much easier to remove inverted commas in String type :)