zoukankan      html  css  js  c++  java
  • 使用Spring boot开发RestFul风格项目PUT/DELETE方法不起作用

    在使用Spring boot 开发restful 风格的项目,put、delete方法不起作用,解决办法。

    实体类Student

    @Data
    public class Student {
    
        private String id;
        private String name;
        private int age;
        private String sex;
    
        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }

    controller

    @RestController
    public class GreetingController {
    
        @RequestMapping(value = "/demo1/student", method = RequestMethod.POST)
        public void addStudent(Student student) {
            System.out.println(student);
            System.out.println("添加成功");
        }
    
        @RequestMapping(value = "/demo1/student", method = RequestMethod.PUT)
        public void updateStudent(Student student) {
            System.out.println(student);
            System.out.println("更新成功");
        }
    
        @RequestMapping(value = "/demo1/student", method = RequestMethod.GET)
        public Student getAtudentById(String id) {
            System.out.println("接收到的的参数" + id);
            Stuent s = new Student();
            s.setId("12");
            s.setName("google");
            s.setAge(12);
            return s;
        }
    
    
        @RequestMapping(value = "/demo1/student", method = RequestMethod.DELETE)
        public void delStudent(Student student) {
            System.out.println(student);
            System.out.println("删除成功");
        }
    
    }

    使用put 更新接口调用,参数无法传递过去。解决办法是在需要组装的参数前面添加注解 @RequestBody 

    修改如下:

    @RequestMapping(value = "/demo1/student", method = RequestMethod.PUT)
        public void updateStudent(@RequestBody Student student) {
            System.out.println(student);
            System.out.println("更新成功");
        }

    即可成功调用。

     delete 接口修改方法相同。

    链接来自:https://www.cnblogs.com/zhaopengcheng/p/8192567.html

  • 相关阅读:
    .netcore利用DI实现级联删除
    识别手写数字增强版100%
    嗨!请查收这道有趣的面试题
    理解TCP/IP协议栈之HTTP2.0
    基于Redis的分布式锁和Redlock算法
    从生日悖论谈哈希碰撞
    Redis面试热点工程架构篇之数据同步
    Redis面试热点之底层实现篇(续)
    saltstack安装+基本命令
    25个iptables常用示例
  • 原文地址:https://www.cnblogs.com/nizuimeiabc1/p/14855931.html
Copyright © 2011-2022 走看看