programing

Spring MVC에 필요한 String 매개 변수가 없습니다.

bestprogram 2023. 10. 19. 22:40

Spring MVC에 필요한 String 매개 변수가 없습니다.

Spring MVC에서 컨트롤러에 AJAX 쿼리를 시도합니다.

내 행동 코드는:

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
    //some code    
}

제 Ajax 쿼리는 다음과 같습니다.

$.ajax({
        type: "POST",
        url:url,
        contentType: "application/json",
        data:     {
                start_date:   scheduler.getEvent(id).start_date,
                end_date:  scheduler.getEvent(id).end_date,
                text: scheduler.getEvent(id).text,
                userId: userId
        },
        success:function(result){
         //here some code
        }
    });

그러나 오류가 발생했습니다.

필수 문자열 매개 변수 'start_date'이(가) 없습니다.

왜요? 제가 알기로는.(@RequestParam(value = "start_date") String start_date

UDP
지금 나는 404 나의 수업을 데이터를 듣도록 합니다.

public class EventData {
    public String end_date;
    public String start_date;
    public String text;
    public String userId;
    //Getters and setters
}

제 js AJAX 콜은:

$.ajax({
    type: "POST",
    url:url,
    contentType: "application/json",
    // data: eventData,
    processData: false,
    data:    JSON.stringify({
        "start_date":   scheduler.getEventStartDate(id),
        "end_date":  scheduler.getEventEndDate(id),
        "text": scheduler.getEventText(id),
        "userId": "1"
    }),

컨트롤러 작업:

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody EventData eventData){    
}

그리고 JSON 데이터는 다음과 같습니다.

end_date: "2013-10-03T20:05:00.000Z"
start_date: "2013-10-03T20:00:00.000Z"
text: "gfsgsdgs"
userId: "1"

서버측에서는 요청 파라미터를 쿼리 문자열로 예상하지만 클라이언트측에서는 json 개체를 보냅니다.json을 바인딩하려면 모든 매개 변수를 포함하는 단일 클래스를 생성하고 @RequestParam 대신 @RequestBody 주석을 사용해야 합니다.

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody CommandBean commandBean){
    //some code
}

여기 더 자세한 설명이 있습니다.

저도 같은 문제가 있었습니다.저는 포스트 요청에 config params를 지정하여 해결하였습니다.

var config = {
    transformRequest : angular.identity,
    headers: { "Content-Type": undefined }
}

$http.post('/getAllData', inputData, *config*).success(function(data,status) {
    $scope.loader.loading = false;
})

config는 내가 포함한 매개 변수였고 작동하기 시작했습니다.도움이 되길 바랍니다 :)

스프링 부트 코드

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
    //some code    
}

보낼 우체부 요청 링크:Parmas를 사용하여 postman에 매개변수와 값을 추가하고 아래 요청 링크를 참조하십시오.

http://localhost:8080/events/add?start_date=*someValue*&end_date=*someValue*&text=*someValue*&userId=*someValue*

언급URL : https://stackoverflow.com/questions/19619088/required-string-parameter-is-not-present-error-in-spring-mvc