415에러 해결하기
문제발생
ajax로 뷰를 만들다가 오류가 발생했다.
컨트롤러
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@PostMapping(value = "/signup")
public ResponseEntity<String> submitSignUp(@Valid @RequestBody MemberDto.PostDto postDto)
throws MessagingException, UnsupportedEncodingException {
log.info("##### CREATE MEMBER #####");
Member member = mapper.memberPostDtoToMember(postDto);
Member savedMember = memberService.createMember(member);
URI location = UriCreator.createUri(MEMBER_DEFAULT_URL, savedMember.getMemberId());
mailService.sendEmail(savedMember.getEmail(), savedMember.getMailKey(), savedMember.getMemberId());
return ResponseEntity.ok().body("\"이메일 인증을 해주세요!\"");
}
ajax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function submitFormSignUp() {
var formData = {
name: $("#name").val(),
email: $("#email").val(),
password: $("#password").val()
};
console.log(JSON.stringify(formData));
$.ajax({
type: 'post',
url: '/members/signup',
data: JSON.stringify(formData),
contentType : 'application/json; charset=utf-8',
}).done(function(response){
alert(response);
window.location.href = '/loginForm';
}). fail(function(error) {
console.error("에러:", error);
alert("에러: " + JSON.stringify(error));
});
}
jsp
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
31
32
33
34
35
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<%@ include file="/WEB-INF/views/menu.jsp" %>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script type="text/javascript" src="/js/signUp.js"></script>
<link rel="stylesheet" href="<c:url value='/css/signUp.css'/>">
</head>
<body>
<form name="sign" action="/members/signup" method="post">
<h2>회원가입</h2>
<BR><BR>
<p>
<label for="email">이메일
<input type="email" id="email" name="email" required>
<input type="button" value="중복확인"onclick="checkEmail()"></label>
</p>
<label for="name">이름</label>
<input type="text" id="name" name="name" required>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" required>
<button onclick="submitFormSignUp()">가입하기</button>
</form>
</body>
</html>
이렇게 회원가입을 진행했더니
415에러가 났다.
이 에러는 지원하지않는 형식으로 클라이언트가 요청해서 서버가 거부한 오류이다.
그래서 관리자 도구를 확인해 보니
이렇게 content-type이 맞지않았다.
하지만 ajax에서는 “application.json”;으로 요청을 보냈는데 왜 이런 오류가 났는지 이해가 안됐다.
문제해결과정
검색해보니 서버에서 @RequestBody는 json이나 XML과 같은 형태의 데이터를
Jackson 등의 MessageConverter를 활용하여 Java Object로 변환한다고 한다.
따라서 @RequestBody 대신 @ModelAttribute,@RequestParam을 써서 해결하는 방법이 있었다.
하지만 나의 문제는 클라이언트가 json으로 요청을 보냈는데
계속 application/x-www-form-urlencoded으로 보내서 오류가 나는 것이었다.
알고보니 form 태그를 사용하여 전송할 경우 Default ContentType은 application/x-www-form-urlencoded라고 한다.
따라서 jsp에서 form태그 대신 div로 바꿨더니
jsp
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
31
32
33
34
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<%@ include file="/WEB-INF/views/menu.jsp" %>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script type="text/javascript" src="/js/signUp.js"></script>
<link rel="stylesheet" href="<c:url value='/css/signUp.css'/>">
</head>
<body>
<div class="sign" action="/members/signup" method="post">
<h2>회원가입</h2>
<BR><BR>
<p>
<label for="email">이메일
<input type="email" id="email" name="email" required>
<input type="button" value="중복확인"onclick="checkEmail()"></label>
</p>
<label for="name">이름</label>
<input type="text" id="name" name="name" required>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" required>
<button onclick="submitFormSignUp()">가입하기</button>
</div>
</body>
</html>
content-Type이 json으로 요청이 잘보내졌다!!
느낀점
jsp가 문제라고 생각하지 못하고 계속 ajax와 controller 코드만 바꿔봤는데
jsp가 문제여서 허탈했다. jsp공부를 좀 더 해야될 것같다.
This post is licensed under CC BY 4.0 by the author.