【发布时间】:2014-06-16 16:42:47
【问题描述】:
我正在使用 Restlet 框架来实现对 REST 资源的 POST 请求,该资源应该接受 JSON 格式的数据。问题是,我不断收到415 Unsupported Media Type 错误。
奇怪的是,我在负责处理 POST 请求的函数内部设置了一个断点,当输入为 application/json 时,调试不会在断点处停止(意味着处理 POST 请求的函数是甚至没有被调用,并且错误只是提前出现)。但是,如果我将输入更改为multipart/form-data 或application/x-www-form-urlencoded,调试会在断点处停止。那么当输入为application/json 类型时,为什么不调用POST 函数呢?
这是请求:
POST /res2 HTTP/1.1
Host: localhost:8888
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 121a2782-0b4e-f592-8d78-26f07862d5fd
{"id":3,"name":"John Smith","age":23,"gender":"Male"}
输出的 HTML 消息指出:
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.
主应用代码:
package com.poc.hw11;
import xyz (trimming to save space)
public class JSON_POC extends Application
{
@Override
public Restlet createInboundRoot()
{
Router router = new Router(getContext());
router.attach("/res1", Resource1.class);
return router;
}
}
Resource1 类:
package com.poc.hw11;
import xyz (trimming to save space);
public class Resource1 extends ServerResource
{
@Post
public void addPerson() {
Request request = getRequest(); // BREAKPOINT SET ON THIS LINE. DEBUG DOESN'T REACH THIS POINT WHEN INPUT IS OF application/json TYPE
Response response = getResponse();
//Rest of code here.
}
}
我也尝试将@Post 更改为@Post("json"),但结果是一样的.. 有什么想法吗?
【问题讨论】:
标签: java json rest post restlet