简体   繁体   English

始终返回 HttpStatus 200(确定)

[英]Always return HttpStatus 200 (OK)

We obtain a list of data from a SQL Server database.我们从 SQL 服务器数据库中获取数据列表。 I want to return the list when there is data, but when there is not, I want to return a "No Content" status.当有数据时我想返回列表,但是当没有数据时,我想返回一个“无内容”状态。 My code:我的代码:

public class Main {
  public static void main(String[] args) {
    var main = new Main();
    var result = main.controllerMethod();
    System.out.println("Result: " + result.blockingGet());
  }

  public Flowable<Person> personList(){
    List<Person> repositoryList = List.of();

    return repositoryList
        .stream()
        .collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
          if(list.isEmpty()) return Flowable.empty();
          else return Flowable.fromIterable(list);
        }));
  }

  public Maybe<ResponseEntity<Flowable<Person>>> controllerMethod(){
    var httpStatus =
        new AtomicReference<>(HttpStatus.OK);

    return Maybe.just(personList()
        .switchIfEmpty(subs -> Completable.fromAction(() ->
                httpStatus.set(HttpStatus.NO_CONTENT)).toFlowable()))
        .map(person -> ResponseEntity.status(httpStatus.get())
            .body(person));
  }
}

result:结果:

Result: <200 OK OK,io.reactivex.rxjava3.internal.operators.flowable.FlowableSwitchIfEmpty@35f983a6,[]>结果:<200 OK OK,io.reactivex.rxjava3.internal.operators.flowable.FlowableSwitchIfEmpty@35f983a6,[]>

It seems that you expect httpStatus.set(HttpStatus.NO_CONTENT) to be run, and after running, the status can be read with the correct statuscode.似乎您希望运行 httpStatus.set(HttpStatus.NO_CONTENT) ,运行后,可以使用正确的状态码读取状态。 I'm not very familiar with reactive, but by looking at Completable.java I don't think the Action that you provide in Completable::fromAction is run right away.我对反应不是很熟悉,但是通过查看 Completable.java 我认为您在 Completable::fromAction 中提供的 Action 不会立即运行。

There is probably a more reactive-like way to solve the problem, but I can't help you there.可能有一种更像反应式的方法来解决这个问题,但我帮不了你。 Instead, here's a solution that might work (or at least get you going in the right direction).相反,这里有一个可能有效的解决方案(或者至少让您朝着正确的方向前进)。 Just set the value of the status explicitly yourself before returning your Flowable:在返回你的 Flowable 之前,你自己明确地设置状态的值:

public Flowable<Person> personList(AtomicReference<HttpStatus> reference) {
    List<Person> repositoryList = List.of();

    return repositoryList
            .stream()
            .collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
                if (list.isEmpty()) {
                    reference.set(HttpStatus.NOT_FOUND);
                    return Flowable.empty();
                }
                return Flowable.fromIterable(list);
            }));
}

public Maybe<ResponseEntity<Flowable<Person>>> controllerMethod() {
    var httpStatus = new AtomicReference<>(HttpStatus.OK);

    return Maybe.just(personList(httpStatus))
            .map(person -> ResponseEntity.status(httpStatus.get()))
            .body(person));
}

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

相关问题 Spring Boot返回&#39;HTTP / 1.1 200&#39;而不是&#39;HTTP / 1.1 200 OK&#39; - Spring boot return 'HTTP/1.1 200' not 'HTTP/1.1 200 OK' Junit httpStatus.OK 和 httpStatus.Bad_Request 的测试用例 - Junit Test case for httpStatus.OK and httpStatus.Bad_Request Java客户端的OPTIONS方法始终返回200 / OK - OPTIONS method with Java client returns 200/OK always 如果 HTTPStatus 代码不等于 200,则响应实体为 null - Response Entity is null if the HTTPStatus Code is not equal to 200 Spring MVC是否可以返回不是从HttpStatus枚举返回的HttpStatus状态 - Is it possible to return HttpStatus status which is not from HttpStatus enum in Spring MVC 响应Stripe 200 Ok - Respond to Stripe 200 Ok 即使输入了错误的密码并且文件未保存在服务器上,我也总是得到服务器响应正常200 - I get always Server Response ok 200 even if I give the wrong password and files are not saved on server 为什么全局异常处理程序总是返回200状态? - Why Global exception handler always return 200 status? 码头和Dropwizard:如何始终返回200,而不是404、500等 - Jetty and Dropwizard: how to return always 200 instead of 404, 500, etc 更好的方法返回HttpStatus代码Spring REST - Better approach to return HttpStatus code Spring REST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM