Post

Spring) Spring Boot Request Data

Spring) Spring Boot Request Data

This post was migrated from Tistory. You can find the original here.

RequestParam

1
2
3
4
5
6
7
8
9
10
11
12
(1)
    @PostMapping("/test3")
    public String p2(@RequestParam PostRequestDto requestDto) {
        System.out.println(requestDto.toString());
        return "";
    }
(2)
    @PostMapping("/test")
    public String p(PostRequestDto requestDto) {
        System.out.println(requestDto.toString());
        return "";
    }

When receiving a DTO from a header with the application/x-www-form-urlencoded content type, (1) doesn’t get populated but (2) does. (The DTO needs setters for this to work.)

If it’s not a DTO, both (1) and (2) get populated.

1
2
3
4
5
6
7
public class UserSignUpDto {
    private String username;
    private String password;
    private String email;
    private boolean admin = false;
    private String adminToken = "";
}

For a boolean type, you just need to send text matching true, True, tRUE, etc.

RequestBody

1
2
3
4
5
    @PostMapping("/test2")
    public String g2(@RequestBody PostRequestDto requestDto) {
        System.out.println(requestDto.toString());
        return "";
    }

For the body case, you need to actually use @RequestBody for it to be populated.

?

What exactly does @RequestParam capture? (let’s not use it for now)

This post is licensed under CC BY-NC 4.0 by the author.