本文介绍了Spring MVC:如何在ResponseEntity主体中返回不同的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的请求处理程序中,我想做一些验证,并根据验证检查的结果,我将返回不同的响应(成功/错误)。所以我为响应对象创建了一个抽象类,并为故障情况和成功案例创建了2个子类。代码看起来像这样,但它不会编译,抱怨errorResponse和successResponse无法转换为AbstractResponse。



我对Java通用和Spring很新MVC,所以我不知道一个简单的方法来解决这个问题。

  @ResponseBody ResponseEntity< AbstractResponse> createUser(@RequestBody String requestBody){
if(!valid(requestBody){
ErrorResponse errResponse = new ErrorResponse();
//填充错误信息
return new ResponseEntity< >(errResponse,HTTPStatus.BAD_REQUEST);
}
createUser();
CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();
//填充更多信息
return新的ResponseEntity<>(successResponse,HTTPSatus.OK);
}


解决方案

这里有两个问题:




  • 您的返回类型必须更改为匹配两个响应子类 ResponseEntity

  • 当您实例化ResponseEntity时,不能使用简化的<>语法,您将要使用的响应类 new ResponseEntity< ErrorResponse>(errResponse,HTT PStatus.BAD_REQUEST);

      @ResponseBody ResponseEntity  if(!valid(requestBody){
    ErrorResponse errResponse = new ErrorResponse();
    //填充错误信息
    return new ResponseEntity< ErrorResponse>(errResponse,HTTPStatus.BAD_REQUEST);
    }
    createUser();
    CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();
    //填充更多信息
    return新ResponseEntity< CreateUserSuccessResponse>(successResponse,HTTPSatus.OK);
    }



In my request handler I want to do some validation and based on the outcome of validation check I will return different response (success/error). So I create a abstract class for the response object and make 2 subclasses for failure case and successful case. The code looks something like this, but it doesn't compile, complaining that errorResponse and successResponse cannot be converted to AbstractResponse.

I'm quite new to Java Generic and Spring MVC so I don't know of a simple way to solve this.

@ResponseBody ResponseEntity<AbstractResponse> createUser(@RequestBody String requestBody) {
    if(!valid(requestBody) {
        ErrorResponse errResponse = new ErrorResponse();
        //populate with error information
        return new ResponseEntity<> (errResponse, HTTPStatus.BAD_REQUEST);
    }
    createUser();
    CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();
    // populate with more info
    return new ResponseEntity<> (successResponse, HTTPSatus.OK);
}
解决方案

There are two problems here:

  • your return type has to be changed to match the two response subclasses ResponseEntity<? extends AbstractResponse>
  • when you instantiate your ResponseEntity you cannot use the simplified <> syntax you have to specify which response class you are going to use new ResponseEntity<ErrorResponse> (errResponse, HTTPStatus.BAD_REQUEST);

    @ResponseBody ResponseEntity<? extends AbstractResponse> createUser(@RequestBody String requestBody) {
        if(!valid(requestBody) {
            ErrorResponse errResponse = new ErrorResponse();
            //populate with error information
            return new ResponseEntity<ErrorResponse> (errResponse, HTTPStatus.BAD_REQUEST);
        }
        createUser();
        CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();
        // populate with more info
        return new ResponseEntity<CreateUserSuccessResponse> (successResponse, HTTPSatus.OK);
    }
    

这篇关于Spring MVC:如何在ResponseEntity主体中返回不同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 18:01