[英]How to consume different JSON response into a single endpoint for a client in Spring Boot?
I am trying to consume api json response in the client side.我正在尝试在客户端使用 api json 响应。 It is for user authentication.
它用于用户身份验证。 When the correct user name and password is given the following response is achieved:
当给出正确的用户名和密码时,将获得以下响应:
But when wrong user name or password is given, the following response can be seen.但是当输入错误的用户名或密码时,可以看到以下响应。
For the valid user name and password, I have implemented the following method which works fine for that purpose.对于有效的用户名和密码,我已经实现了以下方法,它可以很好地用于该目的。 But I cannot handle the situation when the wrong user name or password is given.
但是当用户名或密码错误时,我无法处理这种情况。
public ResponseEntity<AuthenticateUserOutputModel> getAutheticateUser(
AuthenticateUserInputModel authenticateUserInput) {
String url = AUTHENTICATE_USER;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<AuthenticateUserInputModel> requestEntity = new HttpEntity<>(authenticateUserInput, headers);
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("username",authenticateUserInput.getUsername() );
form.add("password", authenticateUserInput.getPassword());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(form, headers);
ResponseEntity<AuthenticateUserOutputModel> responseEntity = new ResponseEntity<>(HttpStatus.OK);
try {
responseEntity = restTemplate.postForEntity(url, requestEntity,AuthenticateUserOutputModel.class);
} catch (Exception e) {
System.out.println("Error Found");
}finally {
return responseEntity;
}
}
How can I implement this?我该如何实施?
First you have to catch this error inside catch(HttpClientErrorException e)
, inside this catch statement, you can add if with condition e.getStatusCode() == HttpStatus.UNAUTHORIZED
to check is this 401 Unauthorized , then you know that something with your Authorization is wrong (password or username) and inside this if statement you can do something (return particular information for user or thorw an exception)首先你必须在
catch(HttpClientErrorException e)
中捕获这个错误,在这个 catch 语句中,你可以添加 if 条件e.getStatusCode() == HttpStatus.UNAUTHORIZED
来检查这个401 Unauthorized ,然后你知道你的授权是错误的(密码或用户名),在这个 if 语句中你可以做一些事情(为用户返回特定信息或抛出异常)
public ResponseEntity<AuthenticateUserOutputModel> getAutheticateUser(
AuthenticateUserInputModel authenticateUserInput) {
String url = AUTHENTICATE_USER;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<AuthenticateUserInputModel> requestEntity = new HttpEntity<>(authenticateUserInput, headers);
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("username",authenticateUserInput.getUsername() );
form.add("password", authenticateUserInput.getPassword());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(form, headers);
ResponseEntity<AuthenticateUserOutputModel> responseEntity = new ResponseEntity<>(HttpStatus.OK);
try {
responseEntity = restTemplate.postForEntity(url, requestEntity,AuthenticateUserOutputModel.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) {
// Do something when 401
}
} catch (Exception e) {
System.out.println("Error Found");
}finally {
return responseEntity;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.