메모/에러 메모

[ Spring / axios ] JS -> Controller 데이터 통신 오류 MissingServletRequestParameterException

surimi🍥 2021. 3. 18. 23:39
반응형

# 오류 내용

11-Mar-2021 23:26:55.906 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'str' is not present]

+ 400 error

 

axios({
        method: 'get',
        url: "/init",
        data: '아잉',
        dataType: 'json'
    })
    .then(function (response){
            console.log("# init success : " + response);
            console.log(response.data);
	})
	.catch(function (error){
            console.log("# init error : " + error);
	})
    @RequestMapping(value = "init", method = RequestMethod.GET)
    @ResponseBody
    public int initiateServer(@RequestParam String str) {

        System.out.println(str);
        //indexService.initiateServer();
        return 1;
    }

    var randomText = "아 이게 되네";
    
    axios({
        method: 'post',
        url: "/init",
        data: 'randomText=' + randomText,
        dataType: 'text'
    })
        .then(function (response){
            console.log("# init success : " + response);
            console.log(response.data);
        })
        .catch(function (error){
            console.log("# init error : " + error);
        })
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    @ResponseBody
    public int initiateServer(@RequestParam String randomText) {

        System.out.println("JS로부터 받은 텍스트 : " + randomText);
        return 2;
    }

해결

반응형