자바로 서비스 요청을하려고합니다. 나는 새로운 웹과 휴식 서비스. 응답으로 json을 반환하는 휴식 서비스가 있습니다. 다음 코드는 있지만 json을 사용하여 출력을 처리하는 방법을 알지 못하므로 불완전하다고 생각합니다.Java에서 Rest call의 일부로 JSON 응답 받기
public static void main(String[] args) {
try {
URL url = new URL("http://xyz.com:7000/test/db-api/processor");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os = connection.getOutputStream();
//how do I get json object and print it as string
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
도와주세요. 나는 새로운 서비스와 json입니다. 많은 감사드립니다.
OutputStream os = conn.getOutputStream();
os.write(input.getBytes()); // The input you need to pass to the webservice
os.flush();
...
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream()))); // Getting the response from the webservice
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output); // Instead of this, you could append all your response to a StringBuffer and use `toString()` to get the entire JSON response as a String.
// This string json response can be parsed using any json library. Eg. GSON from Google.
}
가 웹 서비스를 타격에 좀 더 명확한 아이디어를 가지고
this에서보세요 :이 이후
, 그것은 당신의 인생을 쉽게. HTTP 요청을 수행하고 HTTP 응답을 원하는 객체 유형으로 변환 한 다음 해당 객체를 반환합니다. https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate –