我想json
使用HttpURLConnection
in java 客户端发送输入,并且请求将由 Spring MVC 库处理jacksonhaus
以将输出响应为json
URL url = new URL("http://www.test.com/SpringMVC/rest/service/getEmpDetails");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"name\":\"emp1\",\"add\":\"emp2\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
当我使用此代码时,我得到
HTTP错误代码:500
在 Spring MVC 中,我使用模型属性作为输入请求。谁可以帮我这个事。