Springboot PostMapping无法获取数据问题及解决

 更新时间:2022年05月06日 15:12:32   作者:小乔美哒哒  
这篇文章主要介绍了Springboot PostMapping无法获取数据问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

PostMapping无法获取数据问题

在使用SpringBoot的PostMapping注解的时候,发现无法获取数据(get方法可行),经过一番查证,发现需要添加新的注解

举例如下

    //接受单个参数,使用RequestParam,并且添加上name属性,保证前后端的参数名称一致
 
    @PostMapping(value = "/users")
    public RestfulResponse postUser(@RequestParam("id") Integer id,         @RequestParam("username") String username, @RequestParam("password") String password) {
        User user = new User(id, username, password);
        //User user = new User(1,"tom","123123");
        System.out.println(id + "----" + username);
            restfulResponse = new RestfulResponse(true,200,"查询成功", null);
        return restfulResponse;
    }
    //接受一个实体类,要使用RequestBody 注解
    @PostMapping(value = "/getuser")
    public RestfulResponse postUser1(@RequestBody User user) {
        restfulResponse = new RestfulResponse(true,200,"查询成功", user);
        return restfulResponse;
    }

Springboot之PostMapping 

@PostMapping

映射一个POST请求

Spring MVC新特性

提供了对Restful风格的支持

@PostMapping(value = "/user/login")
//等价于
@RequestMapping(value = "/user/login",method = RequestMethod.POST)

扩展

  • @GetMapping,处理get请求
  • @PostMapping,处理post请求
  • @PutMapping,处理put请求
  • @DeleteMapping,处理delete请求

@RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

属性

  • value:指定请求的实际地址
  • method:指定方法类型,get、post、put、delete等
  • consumes:指定处理请求的提交内容类型,如application/json, text/html;
  • produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
  • params: 指定request中必须包含某些参数值是,才让该方法处理。
  • headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

value/method示例

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
    private AppointmentBook appointmentBook;
    
    @Autowired
    public AppointmentsController(AppointmentBook appointmentBook) {
        this.appointmentBook = appointmentBook;
    }
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Appointment> get() {
        return appointmentBook.getAppointmentsForToday();
    }
    @RequestMapping(value="/{day}", method = RequestMethod.GET)
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
        return appointmentBook.getAppointmentsForDay(day);
    }
    @RequestMapping(value="/new", method = RequestMethod.GET)
    public AppointmentForm getNewForm() {
        return new AppointmentForm();
    }
    @RequestMapping(method = RequestMethod.POST)
    public String add(@Valid AppointmentForm appointment, BindingResult result) {
        if (result.hasErrors()) {
            return "appointments/new";
        }
        appointmentBook.addAppointment(appointment);
        return "redirect:/appointments";
    }
}

value的uri值为以下三类:

A) 可以指定为普通的具体值;

B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);

C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
  public void handle(@PathVariable String version, @PathVariable String extension) {    
    // ...
  }
}

cousumes的样例:

@Controller
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {    
    // implementation omitted
}

params的样例:

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
  @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}

headers的样例:

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 基于Java制作一个好玩的打飞机游戏

    基于Java制作一个好玩的打飞机游戏

    这篇文章主要介绍了基于Java制作的打飞机小游戏,这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Java使用Armitage进行渗透测试的方法

    Java使用Armitage进行渗透测试的方法

    在网络安全领域,渗透测试是一种重要的安全评估手段,它通过模拟恶意黑客的行为来检测目标系统是否存在安全漏洞,Armitage是一个基于Java的图形化渗透测试工具,本文将介绍如何使用Armitage进行渗透测试,需要的朋友可以参考下
    2024-12-12
  • 举例解析Java多线程编程中需要注意的一些关键点

    举例解析Java多线程编程中需要注意的一些关键点

    这篇文章主要介绍了Java多线程编程中需要注意的一些关键点,包括ThreadLocal变量与原子更新等一些深层次的内容,需要的朋友可以参考下
    2015-11-11
  • Mybatis查询返回Map<String,Object>类型实例详解

    Mybatis查询返回Map<String,Object>类型实例详解

    这篇文章主要给大家介绍了关于Mybatis查询返回Map<String,Object>类型的相关资料,平时没太注意怎么用,今天又遇到了总结记录一下,方便以后处理此类问题,需要的朋友可以参考下
    2022-07-07
  • java输入时如何通过回车(enter)来结束输入

    java输入时如何通过回车(enter)来结束输入

    这篇文章主要介绍了java输入时如何通过回车(enter)来结束输入,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Java运行时动态生成对象的方法小结

    Java运行时动态生成对象的方法小结

    Java是一门静态语言,通常,我们需要的class在编译的时候就已经生成了,为什么有时候我们还想在运行时动态生成class呢?今天通过本文给大家分享Java运行时动态生成对象的方法小结,需要的朋友参考下吧
    2021-08-08
  • SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法

    SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http

    这篇文章主要介绍了SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • java selenium 操作浏览器实例

    java selenium 操作浏览器实例

    本文主要介绍java selenium 操作浏览器,这里整理了相关资料,并附示例代码,有需要的小伙伴可以参考下
    2016-08-08
  • vscode开发maven的javaweb项目并部署到tomcat及配置指南

    vscode开发maven的javaweb项目并部署到tomcat及配置指南

    这篇文章主要给大家介绍了关于vscode开发maven的javaweb项目并部署到tomcat及配置的相关资料,在vscode中创建maven项目,需要逐一操作下面的环节,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • java基面试础知识详解

    java基面试础知识详解

    这篇文章主要介绍了java基面试础知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08

最新评论