0

I'm able to parse nested json string with ObjectMapper class. The problem here is it's getting difficult to parse the nested json as I had to write lot of boilerplate code to achieve the end result of parsing json to java objects. Is there any better way of parsing in spring other than this approach?

Domain classes :

Components class

public class Components {

    private String name;

    private String url;

    private List<Properties> properties;

    // getters and setters
}

properties class :

public class Properties {

    private String name;

    private String value;

    private boolean exclusions;

    // getters and setters

} 

Controller class:

@RestController
public class TempController {

    String jsonResponse = null;

    @RequestMapping("/")
    public @ResponseBody String getResponse(){
        JsonreadApplication ja = new JsonreadApplication();
        jsonResponse = ja.readJsonFile();
        //System.out.println(jsonResponse);
        return jsonResponse;
    }

    @RequestMapping("/temp")
    public @ResponseBody String getTempByServerType(String jsonResponse){
        jsonResponse = getResponse();
        Components components = new Components();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Iterator<Entry<String, JsonNode>> fieldNames = objectMapper.readTree(jsonResponse).fields();
            while(fieldNames.hasNext()){
                System.out.println(fieldNames.next());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "I've parsed json!";
    }
}

json file :

{
    "name": "CatalogTools",
    "url": "/atg/commerce/catalog/CatalogTools/",
    "properties": [
        {
            "name": "queryASAFFabrics",
            "value": "skuType=\"ASAF_FABRIC\" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand",
            "exclusions": "false"
        },
        {
            "name": "loggingDebug",
            "value": "true",
            "exclusions": "false"
        }
    ]
}

As said earlier, parsing can be done with ObjectMapper instance but with lot of boilerplate code and hardcoding json element names. Is there a better way of doing this? I'm using jackson library to parse the json file.

I've referred many links online where @RequestBody annotation was used for HTTP POST operations. Here, it is a HTTP GET request.

Any help is highly appreciated.

Many Thanks in advance.

harshavmb
  • 3,404
  • 3
  • 21
  • 55
  • Did you try `public @ResponseBody String getTempByServerType(@RequestBody Components components)`? – Andreas Aug 28 '16 at 21:27
  • @RequestBody annotation is used for only http POST operations. Here, I'm using GET operation, I couldn't find proper link for my question. It would be great if you can pass me reference links where this question is answered. – harshavmb Aug 28 '16 at 21:32
  • You should *not* send a complex JSON (or any JSON) as query parameter on a GET request. Main reason: [URLs are limited in size](http://stackoverflow.com/q/417142/5221149). But, if you absolutely must, did you try `readValue()` instead of `readTree()`? – Andreas Aug 28 '16 at 21:37
  • I'm sorry, I'm new to spring. I've not passed the params to getTempByServerType as an url, have passed as a string. If required, I can remove that as well as I already declared the variable jsonResponse globally. Any other reason why I shouldn't send complex json apart from size? – harshavmb Aug 28 '16 at 21:40
  • @Andreas, thanks for answering, yeah.. I can try other possible methods in ObjectMapper instance, but is there a way I can skip it by using spring-jackson APIs with less code? Thanks again. – harshavmb Aug 28 '16 at 21:42
  • What do you mean *"I've not passed the params to getTempByServerType as an url"*?? You defined a REST controller, and specified `@RequestMapping("/temp")`, for the explicit purpose of that method responding to a request for URL `http://myserver/myapp/temp`. You're not making any sense. – Andreas Aug 28 '16 at 21:42
  • hmm., I don't know why you are worried about the url size here? I have said that I've not passed the url as param. I've passed the json string as the param. You should read multiple times if you are not clear rather than making comments on other senses. – harshavmb Aug 28 '16 at 21:46
  • I *am* reading the question, and I very clearly see a `@RequestMapping("/temp")` annotation, which means that the method is *intended* to be invoked by the Spring container as a result of an incoming HTTP request. I mean, it's a **REST Controller**. The HTTP request will include the required JSON data, and Spring can parse that data for you, *if* you define the method correctly. You're asking for a simpler way, and there's nothing simpler than letting Spring do all the work for you. – Andreas Aug 28 '16 at 21:53
  • As said earlier, I'm new to Spring and I'm not aware that json data is passed in the HTTP request despite not being passed as a param. Many Thanks for letting me know. I'll use the readValue() method to parse the json. – harshavmb Aug 28 '16 at 21:58
  • @Andreas, I still don't understand how the json object is passed in HTTP GET request. I've put the json data in a separate file and I'm loading the file and then parsing it. I can replace the RestController with Controller annotation if it has caused any confusion. – harshavmb Aug 28 '16 at 22:03
  • Only difference between `@Controller` and [`@RestController`](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html) is that `@RestController` automatically adds `@ResponseBody` to your methods, so you don't have to (though you can anyway, like you're currently doing. Any method in a Controller that is annotation with `@RequestMapping` will be called for HTTP requests, and the parameters are [usually coming from the request](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods). – Andreas Aug 28 '16 at 22:59

1 Answers1

0

You can use jsonchema2pojo.

Put there the json's example, set "Source Type" as JSON, select Annotation style to Jackson 2.x or 1.x and then click at "Zip". Voila, the whole model is generated!

slanecek
  • 808
  • 1
  • 8
  • 24