简体   繁体   English

如何在 Mono / Flux 中添加自定义响应 Spring web 通量?

[英]How to add Custom response in Mono / Flux in Spring web flux?

I am new to Spring web flux and looking for a way to add the custom object in Mono response.我是Spring web flux的新手,正在寻找一种在Mono响应中添加自定义 object 的方法。

I need to be my response something like this:我需要做出这样的回应:

{ 
  "success" : "true",
  "data": "<Some Object> or any constant value",
  "error": "If any exception occurs"
}

I tried to use ResponseEntity class with HttpStatus object as follow:我尝试将ResponseEntity class 与HttpStatus object 一起使用,如下所示:

@PostMapping("/v1/cat")
public Mono<ResponseEntity<Long>> createCategory(@RequestBody Categories services) throws Exception {
    return Mono.just(categoriesService.addNewCategory(services)).map(result -> new ResponseEntity<>(result
    , HttpStatus.OK)).defaultIfEmpty(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}

The response I am getting is 15944280045 Ie long value but without HttpStatus.OK .我得到的响应是15944280045即长值但没有HttpStatus.OK How can I adapt the above response in the desired response object?如何在所需的响应 object 中调整上述响应?

Any help would be appreciable.任何帮助都是不言而喻的。

You can implement this way:你可以这样实现:

I assume categoriesService.addNewCategory returns _id , null or throws Exception我假设categoriesService.addNewCategory返回_id , null 或抛出Exception

@PostMapping("/v1/cat")
public Mono<ResponseEntity<ResponseDto<String>>> createCategory(@RequestBody Categories services) throws Exception {
    return Mono
            .just(services)
            .map(categoriesService::addNewCategory)
            .map(result -> new ResponseEntity<>(ResponseDto.success(result), HttpStatus.OK))
            //Couldn't reproduce
            //.defaultIfEmpty(new ResponseEntity<>(ResponseDto.fail("not found", String.class), HttpStatus.BAD_REQUEST))
            .onErrorResume(
                throwable -> Mono.just(new ResponseEntity<>(ResponseDto.fail(throwable.getMessage(), 
                    String.class), HttpStatus.BAD_REQUEST)));
}

On Success ( HTTP success )成功( HTTP 成功

{
  "success": true,
  "data": "5f09f5ac102c3c32c4203d8c",
  "error": null
}

On Error出错时

{
  "success": false,
  "data": null,
  "error": "Cat4 already exists"
}

ResponseDto class对 class 的响应 D

public class ResponseDto<T> {

    private Boolean success;
    private T data;
    private String error;
    
    private ResponseDto(Boolean success, T data, String error) {
        this.success = success;
        this.data = data;
        this.error = error;
    }
    
    public static <T> ResponseDto<T> success(T data) {
        return new ResponseDto<T>(true, data, null);
    }
    
    @SuppressWarnings("unchecked")
    public static <T, S> ResponseDto<T> fail(String error, S clazz) {
        return (ResponseDto<T>) new ResponseDto<S>(false, null, error);
    }
    
    public Boolean getSuccess() {
        return success;
    }
    public void setSuccess(Boolean success) {
        this.success = success;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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