728x90
리액트에서 데이터를 넘겨받았더니 이런 에러가 발생했다.
WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
// 스터디별 List 출력
@RequestMapping(value = "calendarSelectStudy.do", method = RequestMethod.POST)
@ResponseBody
public List<CalendarDto> calendarSelectStudy(HttpSession session, @RequestBody int studyGroupId) {
logger.info("[Controller] calendarSelectStudy.do");
study_group_id = study_group_id.split("=")[0]+"";
System.out.println("SELECTSTUDY TEST : " + studyGroupId);
return calendarBiz.calendarSelectStudy(studyGroupId);
}
아무리 봐도 잘못된 부분이 없는거같은데 왜 안되는 것인가...
415에러가 자꾸 발생한다.
리액트에서 data로 number타입의 studyGroupId 하나를 넘겨서 스프링에서 int타입으로 받아 줬는데 여기서는 이렇게 하면 안된다고한다.
CalendarService.CalendarSelectStudy = (data) => {
return ApiService.post("calendarSelectStudy.do", data);
};
ApiService.post = async (uri, body) => {
let resData = {};
console.log("APIservice : ", body);
const config = { "Content-Type": "application/json" };
try {
resData = await axios.post(`${uri}`, body, config);
} catch (error) {
console.error(error);
}
return resData;
};
리액트에서 스프링으로 데이터를 넘길때 어쨋든 제이슨 형태로 값을 보내기 때문...
String 타입으로 받아주면 해결된다.
@RequestMapping(value = "calendarSelectStudy.do", method = RequestMethod.POST)
@ResponseBody
public List<CalendarDto> calendarSelectStudy(HttpSession session, @RequestBody String studyGroupId) {
logger.info("[Controller] calendarSelectStudy.do");
study_group_id = study_group_id.split("=")[0]+"";
System.out.println("SELECTSTUDY TEST : " + studyGroupId);
return calendarBiz.calendarSelectStudy(Integer.parseInt(studyGroupId));
}
근데 왜인지는 모르겠으나 분명 값을 잘 넘겨 받았으나 왜인지 꼭 끝에 = 이 붙어서 받아온다...
원인을 몰라서 일단 강제로 split으로 =을 짤라버렸다
이에 대한 해법은 @RequestBody가 아닌 @RequestParam을 사용하면된다.
CalendarService.CalendarSelectStudy = (data) => {
return ApiService.post("calendarSelectStudy.do?studyGroupId="+data);
};
data를 파라미터로 넘겨주는것이 아닌 쿼리스트링으로 넘겨준다음
@RequestMapping(value = "calendarSelectStudy.do", method = RequestMethod.POST)
@ResponseBody
public List<CalendarDto> calendarSelectStudy(HttpSession session, @RequestParam String studyGroupId) {
logger.info("[Controller] calendarSelectStudy.do");
// study_group_id = study_group_id.split("=")[0]+"";
System.out.println("SELECTSTUDY TEST : " + studyGroupId);
return calendarBiz.calendarSelectStudy(Integer.parseInt(studyGroupId));
}
@RequestParam을 사용하면 끝... 값이 잘 넘어온다.
728x90
'오류&에러 > Spring' 카테고리의 다른 글
스프링 RequestMethod.GET?POST? 405에러 (0) | 2021.04.22 |
---|---|
스프링 에러 org.springframework.beans.factory.BeanCreationException (0) | 2021.04.20 |
[eclipse]Spring 설치후 Dynamic Web Project 생성 오류 (6) | 2021.04.11 |