티스토리 뷰

 

1. 배열 전송하기

Client
Method : POST
Content-type : application/x-www-form-urlencoded; charset=UTF-8

    $.ajax({
        url: "/test",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        type: "post",
        data: {ids : [1,2,3]},
        success: function (res) {
        },
        error: function (request, status, error) {
        }
    });

Server

    @PostMapping(value = "/test")
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void test(
            @RequestParam(name = "ids[]", required = false) List<Integer> ids
    ) {
        logger.info("ids : {}", ids);
    }

2. 배열 전송하기(JSON)

Client
Method : POST
Content-type : application/json; charset=UTF-8

    $.ajax({
        url: "/test",
        dataType: "json",
        contentType: "application/json; charset=UTF-8",
        type: "post",
        data: JSON.stringify([1, 2, 3]),
        success: function (res) {
        },
        error: function (request, status, error) {
        }
    });

Server

    @PostMapping(value = "/test")
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void test(
            @RequestBody(required = false) List<Integer> ids
    ) {
        logger.info("ids : {}", ids);
    }

3. 배열과 데이터 전송하기(JSON)

Client
Method : POST
Content-type : application/json; charset=UTF-8

    $.ajax({
        url: "/test",
        dataType: "json",
        contentType: "application/json; charset=UTF-8",
        type: "post",
        data: JSON.stringify({ids: [1, 2, 3], name: "kim"}),
        success: function (res) {
        },
        error: function (request, status, error) {
        }
    });

Server

Json 데이터를 바인딩할 객체

public class Test {

    private List<Integer> ids;
    private String name;
    
    //setter, getter 생략
    
}
    @PostMapping(value = "/test")
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void test(
            @RequestBody(required = false) Test test
    ) {
        logger.info("test : {}", test);
    }
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함