4

I am a begineer in java development but has previous experience on programming languages like PHP and Python. So little confused on how to proceed on spring boot with the development.

I am developing a rest API which has the following request

{
  "key":"value",
  "key1":"value1",
  "platform_settings":[
      {"key":"value"}
  ]
}

What I did

I created a RestController which accepts the http request and created a function for the resource

public Share share(@RequestBody final Share share) { 
        LOGGER.debug("This is the request", share);
        return share; //
}

Question 1 : If it was any other programming language like PHP or Python, there will be helper function which will accept the json request and convert it to object which I can easily work on.

In python it is as simple as

import json
import requests

response = requests.get(...)
json_data = json.loads(response.text)
//can work on json_data anyway I want.

But in java, I will have to create a POJO class, or have jackson/JPA entity as dependency which will map the request to a Class (Which I should predefine with the requests).

Is there any better way I can do this? For every request I make, I will have to create a Class which the request can be mapped to and I will have to define the class

Entity

package com.payunow.socialsharemodule.models;

import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;


import javax.persistence.Id;


@Entity
public class Share {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String key;
    private String key1;
    private Map<String,String> platform_settings;

    public Share(String name, String description,Map<String,String> platform_settings) {
        this.key = key;
        this.key1 = key1;
        this.platform_settings = platform_settings;
    }

    //for JPA
    public Share() {}

    public String getKey() {
        return key;
    }

    public String getKey1() {
        return key1;
    }

    public Map<String,String> getPlatform_settings() {
        return platform_settings;
    }

}

For every request I make, I will have to create a class defining all its variables inside. Is this the only way to do this?

Ajeesh
  • 5,650
  • 8
  • 30
  • 52
  • @Japu_D_Cret This is not a duplicate. Read my question before you comment please! – Ajeesh Mar 30 '17 at 06:15
  • it kind of is, the accepted answer which I linked to uses the `json-simple` library which does not force you to create a wrapper for your response, you can work on objects like JSONArray and JSONObject and then convert these objects accordingly. But if your response is of the same format every time, I suggest to create a wrapper class an Jackson – Japu_D_Cret Mar 30 '17 at 06:20
  • @Japu_D_Cret If you see the question, my question is about handling request and whether should I always map request to a class or not. And your answer pertains to usage in a J2ME application not a springboot application – Ajeesh Mar 30 '17 at 06:25
  • linked the wrong answer, see http://stackoverflow.com/questions/1688099/converting-json-to-java and here's how to convert a JSON string with json-simple http://stackoverflow.com/questions/30757136/converting-string-to-json-object-using-json-simple – Japu_D_Cret Mar 30 '17 at 06:31
  • 1
    `whether should I always map request to a class or not` if you want to get every last grain of performance out of your response handling then either don't parse the json at all or parse it only as much as needed with json-simple. If you want to work reliably on the response and have a strongly typed object, which is more user-friendly to work with than I suggest that you take the small time deficit and convert it completely to a POJO with Jackson or the like. If you want to work with it like you did with Python, the json-simple library is pretty close – Japu_D_Cret Mar 30 '17 at 06:35
  • to allow StackOverflow users to give you a more detailed answer, please add more details to your use-case. How many requests per minute, how large are the responses, are they analyzed or only in a really specific case(exception)? also sorry for misinterpreting your question at first – Japu_D_Cret Mar 30 '17 at 06:37
  • @Japu_D_Cret I have given a very simple example of the request and the things questions I have. Not sure I can elaborate it more than this. But thanks for taking effort to respond – Ajeesh Mar 30 '17 at 07:02

2 Answers2

10

You need to have Jackson dependecy for coversion of json to java object. But spring provides it by default, so you don't have to add it explicitly.

You don't need a JPA Entity. This is needed only when you want to store the recieved data into database.
Just to recieve the request you don't have to create a separate pojo class. Look at this code

@PostMapping("/json")
public JSONObject getGeneric(@RequestBody String stringToParse){
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        try {
            json = (JSONObject) parser.parse(stringToParse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    return json;
}   

As you can see here it takes a string as a request and converts it into a generic JSONObject. So basically you can pass any json to this endpoint.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
0

You CanUse ObjectMapper class it has methods like convertValue and realValue..