繁体   English   中英

如何在 Spring 引导中将不同的 JSON 响应消耗到客户端的单个端点中?

[英]How to consume different JSON response into a single endpoint for a client in Spring Boot?

我正在尝试在客户端使用 api json 响应。 它用于用户身份验证。 当给出正确的用户名和密码时,将获得以下响应: 在此处输入图像描述

但是当输入错误的用户名或密码时,可以看到以下响应。

在此处输入图像描述

对于有效的用户名和密码,我已经实现了以下方法,它可以很好地用于该目的。 但是当用户名或密码错误时,我无法处理这种情况。

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;
    }
    
}

我该如何实施?

首先你必须在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM