H
H
hellhoundSE2019-08-30 03:51:04
Java
hellhoundSE, 2019-08-30 03:51:04

Is there automatic JSON parsing in java pojo?

I'm still very weak in the rest api, and springboot, but I know that springboot can parse to json and back.
I'm going to make an HTTP request in the code (I get json in response), the question is whether spring itself can make a java object out of it. Sorry if the question is incorrect. Thanks for any replies.

url = new URL("https://httpbin.org/get");
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setRequestMethod("GET");

      int status = con.getResponseCode();
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer content = new StringBuffer();
      while ((inputLine = in.readLine()) != null) {
        content.append(inputLine);
      }
      System.out.println("\n"+content+"\n");
      in.close();
      con.disconnect();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JeRRy_froyo, 2019-08-30
@JeRRy_froyo

Spring has a RestTemplate: https://www.baeldung.com/rest-template

public class Foo implements Serializable {
    private long id;
 
    private String name;
    // standard getters and setters
}

RestTemplate restTemplate = new RestTemplate();
Foo foo = restTemplate.getForObject(url, Foo.class);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question