Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question