简体   繁体   English

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

[英]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.

相关问题 如何使用JSON端点? - How to consume a JSON endpoint? 我们可以在 spring 引导中创建一个可以处理不同请求结构 (JSON) 的 rest API(单端点)吗? - can we create a single rest API(single endpoint) which can handle different request structures (JSON) in spring boot? 如何在 Spring boot 中使用 WebClient 使用二进制和字符串响应? - How to consume binary and String Response using WebClient in Spring boot? 如何在Spring Boot中使用JSON对象数组? - How do I consume an array of JSON objects with Spring Boot? 使用 SOAP 响应始终为空 Spring Boot - consume SOAP Response always null Spring Boot 如何从Spring Boot端点为JavaScript客户端流式传输媒体内容 - How to stream media content from Spring Boot endpoint for javascript client 如何在spring boot中获取索引的json作为响应? - how to get the indexed json as response in spring boot? 如何在 Spring 引导中格式化 JSON 列表响应 - How to format JSON list response in Spring Boot 对于 Spring Boot 中的 GET 请求,如何在单个 JSON 响应中发送 Header 中的一些特定信息和 Body 中的一些信息? - How to send some specific info in Header and some info in Body in a single JSON response, for a GET request in Spring boot? Spring Boot:是否可以将@RequestParam 作为 json 使用? - Spring Boot: Is it possible to consume @RequestParam's as json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM