REST 方法不会 PUT 或 POST 到服务器
我试图让一些 REST 方法在我的 Spring 应用程序中工作,但似乎收效甚微。我显然错过了一些东西,但我无法告诉我它会是什么。这是我的控制器:
@Controller
public class IndexController {
static Logger log = Logger.getLogger(IndexController.class);
@Autowired
private ProvisionService provisionService;
@RequestMapping(value="/home/data", method=RequestMethod.GET,
headers="Accept=application/json")
public @ResponseBody List<Provision> getData() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = null;
if(principal instanceof UserDetails)
username = ((UserDetails)principal).getUsername();
return provisionService.getAllByUser(username);
}
//JSON put request - doesn't work currently
@RequestMapping(value="/home/data", method=RequestMethod.PUT,
headers="Content-Type=application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateProvisions(@RequestBody List<Provision> provisions) {
log.info("Provisions: " + provisions.toString());
}
@RequestMapping(value={"/","/home"}, method=RequestMethod.GET)
public void showIndex() {}
}
这是使用它的 JSP 的主要部分:
<sf:form id="homeForm" method="put" action="${homeData_url}"></sf:form>
当用户单击按钮时,表单通过 Javascript 提交。无论如何,GET 一切正常。我得到 Json 与我的对象列表一起返回,没有问题。然后我使用 Dojo 来展示它,到目前为止一切顺利。但是,当我尝试使用此表单返回 Json 时,我收到 405 - 请求方法“POST”不支持错误。正如你所看到的,我的控制器中有方法处理程序,所以我真的不确定我做错了什么。我已经从 Spring in Action 3 书中取出了这些处理程序,它也类似于一些 Spring 文档和内容所说的操作,但显然我缺少一个关键组件。有人有什么想法吗?
我确实在 web.xml 中映射了 HiddenHttpMethodFilter
,这就是我使用 Spring 表单标签的原因。
无论如何,任何想法或帮助都会受到赞赏。谢谢。
------------------更新------------------
这是我之后的标题单击按钮并获取 405 错误(如果有帮助):
http://localhost:8080/NFI/home
POST /NFI/home HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Referer: http://localhost:8080/NFI/home
Cookie: JSESSIONID=584AC21ADE4F214904B9E7E2370363EF
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: GET, PUT
Content-Type: text/html;charset=utf-8
Content-Length: 1085
Date: Fri, 21 Oct 2011 15:39:26 GMT
I'm trying to get some REST methods working in my Spring app but seem to be running into little success. I'm obviously missing something but I can't tell for the life of me what it would be. Here is my controller:
@Controller
public class IndexController {
static Logger log = Logger.getLogger(IndexController.class);
@Autowired
private ProvisionService provisionService;
@RequestMapping(value="/home/data", method=RequestMethod.GET,
headers="Accept=application/json")
public @ResponseBody List<Provision> getData() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = null;
if(principal instanceof UserDetails)
username = ((UserDetails)principal).getUsername();
return provisionService.getAllByUser(username);
}
//JSON put request - doesn't work currently
@RequestMapping(value="/home/data", method=RequestMethod.PUT,
headers="Content-Type=application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateProvisions(@RequestBody List<Provision> provisions) {
log.info("Provisions: " + provisions.toString());
}
@RequestMapping(value={"/","/home"}, method=RequestMethod.GET)
public void showIndex() {}
}
Here is the main part of JSP that utilizes it:
<sf:form id="homeForm" method="put" action="${homeData_url}"></sf:form>
The form is submitted through Javascript when the user clicks on a button. Anyway, things work fine for the GET. I get Json returned with my List of objects, no problems. I then display that using Dojo and so far so good. However, when I try to return the Json with this form I'm getting a 405 - Request method 'POST' not supported error. As you can see I've got the method handler in my Controller so I'm really not sure what I'm doing wrong. I've taken those handler's out of the Spring in Action 3 book and it also resembles what some Spring docs and stuff say to do, but obviously I'm missing a key component. Anyone have any thoughts?
I do have the HiddenHttpMethodFilter
mapped in my web.xml which is why I'm using the Spring form tag.
Anyway, any thoughts or help are appreciated. Thank you.
------------------UPDATE------------------
Here are the headers after I click on the button and get the 405 error, if it helps:
http://localhost:8080/NFI/home
POST /NFI/home HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Referer: http://localhost:8080/NFI/home
Cookie: JSESSIONID=584AC21ADE4F214904B9E7E2370363EF
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: GET, PUT
Content-Type: text/html;charset=utf-8
Content-Length: 1085
Date: Fri, 21 Oct 2011 15:39:26 GMT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提交表单是使用 POST 完成的。您收到“POST”不支持的错误。
在上面,我看到您在源代码中使用了 RequestMethod.PUT。根本没有提到 POST。
Submitting a Form is done using POST. You get a "POST" not supported error.
Above, I see you are using a RequestMethod.PUT in your source code. There's no mention of POST at all.
添加您需要在请求中添加一个值为
PUT
的参数_method
。不是json内容!因此,在第一步中,我会将请求的 URL 更改为
/home/data?_method=PUT
。如果这项工作有效,您可以寻找一种在不干扰 Json 数据的情况下将 _method 参数添加到请求内容的方法。
Add you need to add a parameter
_method
with valuePUT
to your request. Not to the json content!So in the first step I would change requested URL to
/home/data?_method=PUT
.If this work you can search for an way how to add the
_method
parameter to the request content without disturbing the Json data.您使用标头更新了您的问题,您是否也可以将整个请求放在那里(实际转储的值)以查看发送的 _method 参数?
另外,虽然我猜 headers=""-rules 是有效的,但它们不应该被需要。您有一个 json 转换器 bean,它将根据内容类型进行编组和解组并接受标头,如果找不到有效的转换器,Spring 将返回错误。
将其包含在 @RequestMapping 中的唯一原因是,如果您使用 xml 而不是 json 调用一个实际上执行其他操作的方法,那么这听起来是一个糟糕的设计。
删除这些标头规则并重试,使其尽可能简单并逐渐添加逻辑。
You updated your question with the headers, could you also put the entire request out there (actual dumped values) to see the _method parameter(s) being sent?
Also, while I guess the headers=""-rules are valid they shouldn't be needed. You have a json converter bean that will do marshall and unmarshall based on content-type and accept headers, if no valid converter is found Spring will return an error.
The only reason to include it in the @RequestMapping would be if you had a method that actually did something else if you called it with xml instead of json and that sounds like a bad design.
Remove those header-rules and try again, make it as simple as possible and gradually add logic.