本文介绍了我可以对发布请求使用@Requestparam批注吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器方法:

@PostMapping(
        value = "/createleave",
        params = {"start","end","hours","username"})
public void createLeave(@RequestParam(value = "start") String start,
                        @RequestParam(value = "end") String end,
                        @RequestParam(value = "hours") String hours,
                        @RequestParam(value = "username") String username){
    System.out.println("Entering createLeave " + start + " " + end + " " + hours + " " + username);
    LeaveQuery newLeaveQuery = new LeaveQuery();
    Account account = accountRepository.findByUsername(username);
    newLeaveQuery.setAccount(account);
    newLeaveQuery.setStartDate(new Date(Long.parseLong(start)));
    newLeaveQuery.setEndDate(new Date(Long.parseLong(end)));
    newLeaveQuery.setTotalHours(Integer.parseInt(hours));
    leaveQueryRepository.save(newLeaveQuery);
}

但是,当我向该端点发送发布请求时,我得到以下信息

However when I send a post request to this endpoint I get the following

"{"timestamp":1511444885321,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.UnsatisfiedServletRequestParameterException","message":"Parameter conditions \"start, end, hours, username\" not met for actual request parameters: ","path":"/api/createleave"}"

当我从@PostMapping注释中删除params参数时,我得到了一个更一般的错误,它将说它找不到第一个必需的参数(开始),而实际上它与参数end,hours一起发送了.和用户名.

When I remove the params argument from the @PostMapping annotation I get a more general error, it will say that it cannot find the first required parameter (start), while it really is being send together with the parameters end, hours and username.

如何在Spring MVC之后的方法中获取参数?/a>

how to get param in method post spring mvc?

我在这篇文章中已经读到,@RequestParam仅可用于get方法,但是如果我删除@RequestParam并坚持使用@PostMapping批注的params参数,它仍然不起作用.我知道我可以使用@RequestBody,但是我不想仅仅为这4个参数创建一个类.谁能告诉我如何进行这项工作?

I've read in this post that @RequestParam can only be used for get methods, but if I remove @RequestParam and stick with the params argument of the @PostMapping annotation it still doesn't work. I know I can use @RequestBody but I do not want to make a class just for those 4 parameters. Can anyone tell me how I can make this work?

谢谢

我在这里阅读 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- 参数论证并不完全是我所想的.它似乎用作条件.如果一组参数与某个值匹配,则将激活端点控制器方法.

I'm reading here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- that the argument params isn't exactly what I thought it was. It seems to be used as a condition. If a set of parameters match a value then the endpoint controller method will be activated.

推荐答案

您要求的是根本错误的. POST请求在主体有效负载中发送数据,该有效负载通过@RequestBody进行映射. @RequestParam用于通过URL参数(例如/url?start=foo)映射数据.您想做的是使用@RequestParam来完成@RequestBody的工作.

What you are asking for is fundamentally wrong. POST requests sends data in a body payload, which is mapped via @RequestBody. @RequestParam is used to map data through the URL parameters such as /url?start=foo. What you are trying to do is use @RequestParam to do the job of @RequestBody.

  • 介绍DTO类.这是最优选和最清洁的方法.
  • 如果您真的想避免创建类,则可以使用@RequestBody Map<String, String> payload.确保在请求标头中包含'Content-Type': 'application/json'.
  • 如果您确实要使用@RequestParam,请改用GET请求,然后通过URL参数发送数据.
  • Introduce a DTO class. It is the most preferred and clean method.
  • If you really want to avoid creating a class, you can use @RequestBody Map<String, String> payload. Be sure to include 'Content-Type': 'application/json' in your request header.
  • If you really want to use @RequestParam, use a GET request instead and send your data via URL parameters.
  • 介绍DTO类,并将其与注释@ModelAttribute一起使用.
  • 如果将表单数据转换为JSON,则可以使用@RequestBody Map<String, String> payload.为此,请参见此答案.
  • Introduce a DTO class and use it with annotation @ModelAttribute.
  • If you transform the form data into JSON, you can use @RequestBody Map<String, String> payload. To do this, please see this answer.

不可能将表单数据编码数据直接映射到Map<String, String>.

It is not possible to map form data encoded data directly to a Map<String, String>.

这篇关于我可以对发布请求使用@Requestparam批注吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:27