简体   繁体   中英

Recommended way of sending response object in body for errors

I have a Custom response class

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Getter @Setter @ToString @Builder @AllArgsConstructor @NoArgsConstructor
public class WrapperRestResponse {

    @ApiModelProperty(name="responseCode",notes = "http status response codes")
    private Integer responseCode;
    @ApiModelProperty(name="status",notes = "Success|Failure")
    private String status;
    @ApiModelProperty(name="errors",notes = "In case of Failure, it will contains error messages")
    private List<String> errors;
    @ApiModelProperty(name="body",notes = "In case of Success, it will return object")
    private Object body;
}

A Util function for errors that uses this response class:

public static WrapperRestResponse getErrorResponse(HttpStatus statusCode, String ... errors){
        return WrapperRestResponse.builder()
                .status(WrapperConstant.FAILURE)
                .responseCode(statusCode.value())
                .errors(Arrays.asList(errors))
                .build();
    }

And my controller sends response like this:

@PostMapping
@ApiOperation(value = "Register", notes = "Register")
public WrapperRestResponse register(@RequestBody DTO request) {
        try {
            return Util.getSuccessResponse(HttpStatus.CREATED, Service.create(request));
        } catch (ImproperDataException e) {
           return Util.getErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return Util.getErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

The problem: When the code raises a bad request error response, the status code is still 200. The response looks like the following ( NOTE: responseCode is 400 but statusCode is 200:

{
    "text": "{\"responseCode\":400,\"status\":\"Failure\",\"errors\":[\"The following need to be registered before logging usage against them:[data1]\"]}",
    "statusText": "OK",
    "statusCode": 200,
    "status": 200,
    "statusType": 2,
    
    
    "type": "application/json",
    "charset": "utf-8",
    "links": {},
    "body": {
        "responseCode": 400,
        "status": "Failure",
        "errors": [
            "The following need to be registered before logging usage against them:[data1]"
        ]
    }
}

What would be an elegant Spring way to handle such exceptions and match the statusCode and responseCode ?

Why you are not using Spring's inbuilt class ResponseEntity like below

@PostMapping
@ApiOperation(value = "Register", notes = "Register")
public ResponseEntity<Object> register(@RequestBody DTO request) {
    try {
        return new ResponseEntity<Object>(Service.create(request),HttpStatus.CREATED);
    } catch (ImproperDataException e) {
        return new ResponseEntity<Object>(Arrays.asList(e.getMessage()),HttpStatus.BAD_REQUEST);
    } catch (Exception e) {
        return new ResponseEntity<Object>(Arrays.asList(e.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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