Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@requestBody DTO에서 Enum에 invalid한 값이 넘어올때 #3

Open
LeoHeo opened this issue Apr 1, 2018 · 0 comments
Open

@requestBody DTO에서 Enum에 invalid한 값이 넘어올때 #3

LeoHeo opened this issue Apr 1, 2018 · 0 comments
Assignees
Labels
삽질 삽질한 것들에 대한 기록

Comments

@LeoHeo
Copy link
Owner

LeoHeo commented Apr 1, 2018

삽질의 시작

public enum Provider {
  GENERAL,
  FACEBOOK,
  GOOGLE
}

위와 같은 enum이고 아래같이 invalid한 provider값을 post함

{
	"provider": "test"
}

원인

  • jsonParseError라는 에러가 나면서 @Valid를 로직을 안타서 missing required value라는 에러메세지가 response가 안된다.
@PostMapping("/signup")
public void singup(@Valid @RequestBody UserSaveDto userSaveDto, BindResult result) {
  if (result.hasErrors()) {
    throw new BadRequestException("missing requied value");
  }

  // do somthing..
}

해결

  • enum 클래스에 @JsonCreator를 추가
@JsonCreator
public static Provider create(String requestValue) {
  return Stream.of(values())
            .filter(v -> v.toString().equalsIgnoreCase(requestValue))
            .findFirst()
            .orElse(null)
}
@LeoHeo LeoHeo added the 삽질 삽질한 것들에 대한 기록 label Apr 1, 2018
@LeoHeo LeoHeo self-assigned this Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
삽질 삽질한 것들에 대한 기록
Projects
None yet
Development

No branches or pull requests

1 participant