【发布时间】:2015-07-08 10:24:22
【问题描述】:
我正在尝试用 Java 开发一个接受通用 JSonObject(我正在使用 JSON Simple 1.1.1)的 Rest WebService(我正在使用 RestEasy)。
到目前为止我做了什么:
@Path("/message")
public class TestRestService {
@POST
@Path("/{param}")
@Consumes("application/json")
public Response printMessage(JSONObject inputJsonObj) {
String result = "Restful example : " + inputJsonObj;
System.out.println(result);
return Response.status(200).entity(result).build();
}
}
这是我的客户:
public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/myProject/rest/message/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); String input = "{\"qty\":100,\"name\":\"iPad 4\"}"; 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()); } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { } catch (IOException e) { } }
不幸的是,我收到 405 错误,这意味着我认为的路径不存在...
谁能帮帮我?
谢谢!
【问题讨论】:
-
响应代码 405 是 方法不允许。您是否缺少客户端 URL 上的 {param}?