@RequestBody注解

当接口入参为List形式的为什么一定要加@RequestBody注解,不加的时候postman请求时怎样的形式

@RequestBody   是从请求体中将json字符串转成list

不加@RequestBody的话   可以尝试用form表单提交的方式,或者直接拼在url的方式,127.0.0.1/aaa?userList=user1&userList=user2

form表单提交可能是这样,很久没用到form提交了

 

 

/**
 * Annotation indicating a method parameter should be bound to the body of the web request.
 * The body of the request is passed through an {@link HttpMessageConverter} to resolve the
 * method argument depending on the content type of the request. Optionally, automatic
 * validation can be applied by annotating the argument with {@code @Valid}.
 *
 * <p>Supported for annotated handler methods.
 *
 * @author Arjen Poutsma
 * @since 3.0
 * @see RequestHeader
 * @see ResponseBody
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
 */

这是RequestBody的官方说明,简单翻译一下: RequestBody注解表示controller里面的一个方法应该和一个http请求的body绑定,这个请求体传递过来的参数会经过HttpMessageConverter去解析。

其实也很好理解,既然你要穿一个List,那么肯定不能用基本的@RequestParam的方式,因为这个方式传递的就是一个key=value的参数,而List你是想一下传递一个集合,那么用key=value的形式是无法表达的,所以要把参数放到body中。

public class User {
    private String name;
    private  int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}


@RestController
public class HelloController {

    @RequestMapping("hello")
    public String sayHi( @RequestBody List<User> userList){
           userList.stream().forEach(user -> {
            System.out.println("hello " + user.getName());
        });
        return null;
    }
}

请求:

/**
 * Annotation indicating a method parameter should be bound to the body of the web request.
 * The body of the request is passed through an {@link HttpMessageConverter} to resolve the
 * method argument depending on the content type of the request. Optionally, automatic
 * validation can be applied by annotating the argument with {@code @Valid}.
 *
 * <p>Supported for annotated handler methods.
 *
 * @author Arjen Poutsma
 * @since 3.0
 * @see RequestHeader
 * @see ResponseBody
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
 */

这是RequestBody的官方说明,简单翻译一下: RequestBody注解表示controller里面的一个方法应该和一个http请求的body绑定,这个请求体传递过来的参数会经过HttpMessageConverter去解析。

其实也很好理解,既然你要穿一个List,那么肯定不能用基本的@RequestParam的方式,因为这个方式传递的就是一个key=value的参数,而List你是想一下传递一个集合,那么用key=value的形式是无法表达的,所以要把参数放到body中。

public class User {
    private String name;
    private  int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}


@RestController
public class HelloController {

    @RequestMapping("hello")
    public String sayHi( @RequestBody List<User> userList){
           userList.stream().forEach(user -> {
            System.out.println("hello " + user.getName());
        });
        return null;
    }
}

请求: