我可以对 Post 请求使用 @Requestparam 注释吗? [英] Can I use @Requestparam annotation for a Post request?

查看:125
本文介绍了我可以对 Post 请求使用 @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 参数时,我得到一个更一般的错误,它会说它找不到第一个必需的参数(开始),而它实际上是一起发送的与参数结束,小时和用户名.

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.

如何在 post spring mvc 方法中获取参数?

我在这篇文章中读到 @RequestParam 只能用于获取方法,但是如果我删除 @RequestParam 并坚持使用 @RequestParam 的 params 参数code>@PostMapping 注释它仍然不起作用.我知道我可以使用 @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--参数 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?start=foo等URL参数映射数据.您要做的是使用 @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;有效载荷.请务必在您的请求标头中包含 '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;有效载荷.为此,请参阅此答案.
  • 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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆