How use PUT method in Springboot Restcontroller?

前端 未结 5 1586
花落未央
花落未央 2021-02-09 14:37

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

相关标签:
5条回答
  • 2021-02-09 14:52

    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.

    0 讨论(0)
  • 2021-02-09 14:58

    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

    In this case it will be:

    @PutMapping("/student/info")
    public @ResponseBody String updateStudent(@RequestParam(value = "stdName")String stdName){
        LOG.info(stdName);
        return "ok";
    }
    
    0 讨论(0)
  • 2021-02-09 15:01

    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";
    }
    
    0 讨论(0)
  • 2021-02-09 15:08

    I meet the same issue with spring boot 1.5.*,I fixed it by follow:

    @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

    0 讨论(0)
  • 2021-02-09 15:09

    you can add @RestController annotation before your class.

    @RestController
    @RequestMapping(value = "/v1/range")
    public class RangeRestController {
    }
    
    0 讨论(0)
提交回复
热议问题