Spring 注解面面通 之 @RequestBody

  @RequestBody用于将完整请求正文映射到控制器处理方法的参数中,请求正文通过HttpMessageConverter进行转换。

  注解解析

  ① required:

    请求正文是否必须有值,默认值为true

    requiredtrue时,如果请求正文没有值,则会抛出异常。

    requiredfalse时,如果请求正文没有值,则会返回null

  注解示例

  1)Controller,用来演示@RequestBody使用方法。

package com.arhorchin.securitit.webannotations;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

/**
 * @author Securitit.
 * @note 演示@RequestBody使用方法.
 */
@Controller
@RequestMapping("/WebAnnotations")
public class RequestBodyController {/**
     * logger.
     */
    private Logger logger = LoggerFactory.getLogger(RequestBodyController.class);

    /**
     * 从请求报文以Map解析方法参数.
     */
    @ResponseBody
    @RequestMapping(
            value = "/RequestBodyMap.do",
            method = RequestMethod.POST)
    public String requestParamMap(@RequestBody Map<String, String> requestBodyMap) throws Exception {logger.info("Current value of RequestBody is "   JSON.toJSONString(requestBodyMap));
        return "Current value of RequestBody is "   JSON.toJSONString(requestBodyMap);
    }

    /**
     * 从请求报文以Map解析方法参数.
     */
    @ResponseBody
    @RequestMapping(
            value = "/RequestBodyMapRequiredTrue.do",
            method = RequestMethod.POST)
    public String requestParamMapRequiredTrue(@RequestBody(
            required = true) Map<String, String> requestBodyMap) throws Exception {logger.info("Current value of RequestBody [required is true] is "   JSON.toJSONString(requestBodyMap));
        return "Current value of RequestBody [required is true] is "   JSON.toJSONString(requestBodyMap);
    }

    /**
     * 从请求报文以Map解析方法参数.
     */
    @ResponseBody
    @RequestMapping(
            value = "/RequestBodyMapRequiredFalse.do",
            method = RequestMethod.POST)
    public String requestParamMapRequiredFalse(@RequestBody(
            required = false) Map<String, String> requestBodyMap) throws Exception {logger.info("Current value of RequestBody [required is false] is "   JSON.toJSONString(requestBodyMap));
        return "Current value of RequestBody [required is false] is "   JSON.toJSONString(requestBodyMap);
    }

}

  2) 使用《Spring 注解面面通 之 Http测试工具》提供的Http测试工具进行测试。

  ① 测试http://localhost:9199/spring-annotations/WebAnnotations/RequestBodyMap.do

  ② 测试http://localhost:9199/spring-annotations/WebAnnotations/RequestBodyMapRequiredTrue.do

  可以看到请求返回400Bad Request,查看后端控制台可以看到:

2020-12-05 13:46:10 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.arhorchin.securitit.webannotations.RequestBodyController.requestParamMapRequiredTrue(java.util.Map<java.lang.String, java.lang.String>) throws java.lang.Exception

  ③ 测试http://localhost:9199/spring-annotations/WebAnnotations/RequestBodyMapRequiredFalse.do

  可以看到虽然和中请求一直,但确正常返回数据,原因在于后端设置了@RequestBody(required = false)所致。

  总结

  @RequestBody注解在当下的应用开发中使用非常频繁,掌握它可以让我们开发更加顺畅。

  若文中存在错误和不足,欢迎指正!

来源:https://www.icode9.com/content-4-775551.html

(0)

相关推荐