Java 如何在 Springboot Restcontroller 中使用 PUT 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26625902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How use PUT method in Springboot Restcontroller?
提问by Aravind Cheekkallur
Am developing an application using Spring boot.I tried with all representations verbs like GET, POST , DELETE all are working fine too. By using PUT method, it's not supporting in spring boot. Whether I need to add any new configuration.
正在使用 Spring boot 开发应用程序。我尝试使用所有表示动词,如 GET、POST、DELETE,它们也都工作正常。通过使用 PUT 方法,它在 spring boot 中不支持。我是否需要添加任何新配置。
Put method works only the request not have any parameters. If i add any query parameter or form data it doesnt work. Kindly any expertize will help me to solve this issue.
Put 方法仅适用于没有任何参数的请求。如果我添加任何查询参数或表单数据,它就不起作用。请任何专业人士帮助我解决这个问题。
@RequestMapping("/student/info")
@RequestMapping(method = RequestMethod.PUT)
public @ResponseBody String updateStudent(@RequestParam(value = "stdName")String stdName){
LOG.info(stdName);
return "ok";
}
Request method 'PUT' not supported
不支持请求方法“PUT”
采纳答案by Pratheesh
This code will work fine. You must specify request mapping in class level or in function level.
此代码将正常工作。您必须在类级别或功能级别指定请求映射。
@RequestMapping(value = "/student/info", method = RequestMethod.PUT)
public @ResponseBody String updateStudent(@RequestBody Student student){
LOG.info(student.toString());
return "ok";
}
回答by blackpanther
Have you tried the following Request Mapping:
您是否尝试过以下请求映射:
@RequestMapping(value = "/student/info", method = RequestMethod.PUT)
There's no need to separate the value and the Request Method for the URI.
不需要将 URI 的值和请求方法分开。
回答by PPHAC
you can add @RestController annotation before your class.
您可以在课程之前添加 @RestController 注释。
@RestController
@RequestMapping(value = "/v1/range")
public class RangeRestController {
}
回答by kozielt
Since Spring 4.3 you can use @PutMapping("url")
: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/PutMapping.html
从 Spring 4.3 开始,您可以使用@PutMapping("url")
:https: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/PutMapping.html
In this case it will be:
在这种情况下,它将是:
@PutMapping("/student/info")
public @ResponseBody String updateStudent(@RequestParam(value = "stdName")String stdName){
LOG.info(stdName);
return "ok";
}
回答by GreatC
I meet the same issue with spring boot 1.5.*,I fixed it by follow:
我在 spring boot 1.5.* 中遇到了同样的问题,我通过以下方式修复了它:
@RequestMapping(value = "/nick", method = RequestMethod.PUT)
public Result updateNick(String nick) {
return resultOk();
}
Add this bean
添加这个豆
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory(){
@Override
protected void customizeConnector(Connector connector) {
super.customizeConnector(connector);
connector.setParseBodyMethods("POST,PUT,DELETE");
}
};
}
see also
也可以看看
https://stackoverflow.com/a/25383378/4639921
https://stackoverflow.com/a/47300174/4639921
https://stackoverflow.com/a/25383378/4639921
https://stackoverflow.com/a/47300174/4639921