繁体   English   中英

为什么 Spring/java 不支持 Optional 和 @PathVariable 组合?

[英]Why Optional and @PathVariable combination is not supported by Spring/java?

我想做一个可以接受两个 Path 变量的 api,其中一个变量是可选的。 在这篇文章中,他们说我们可以通过使用 Optional 来实现这一点,但这不起作用。

这是我的控制器

@GetMapping("/users/{dateDu}/{dateAu}")
    public ResponseEntity<List<User>> getAllUsersByDate(
        @PathVariable LocalDate dateDu,
        @PathVariable(required = false) Optional<LocalDate> dateAu,
        @org.springdoc.api.annotations.ParameterObject Pageable pageable
    ) {
        if(dateAu.isEmpty()){
            dateAu = Optional.of(LocalDate.now()); 
        }
        Page<User> page = userRepository.getUsersByDate(dateDu, dateAu.get(), pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

在这张 swagger 的图像中,即使第二个参数有Optional ,这两个参数也是必需的

在此处输入图像描述

由于我使用 JHipster 生成了我的 Spring Boot 应用程序,因此当我没有给出 Optional 参数的值时,在邮递员上。 我收到了这个错误

<!DOCTYPE html>
<html class="no-js" lang="fr" dir="ltr">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title></title>
    <meta name="description" content="Description for " />
    <meta name="google" content="notranslate" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="theme-color" content="#000000" />
    <link rel="icon" href="favicon.ico" />
    <link rel="manifest" href="manifest.webapp" />
    <link rel="stylesheet" href="content/css/loading.css" />
    <!-- jhipster-needle-add-resources-to-root - JHipster will add new resources here -->
    <base href="/">
</head>

<body>
    <!--[if lt IE 9]>
      <p class="browserupgrade">
        You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve
        your experience.
      </p>
    <![endif]-->
    <div id="root">
        <div class="app-loading">
            <div class="lds-pacman">
                <div>
                    <div></div>
                    <div></div>
                    <div></div>
                </div>
                <div>
                    <div></div>
                    <div></div>
                    <div></div>
                </div>
            </div>
        </div>
        <div class="app-loading">
            <div id="jhipster-error" style="display: none">
                <!-- This content is for troubleshooting purpose and will be removed when app renders -->
                <h1>An error has occurred :-(</h1>
                <h2>Usual error causes</h2>
                <ol>
                    <li>
                        You started the application from an IDE and you didn't run
                        <code style="color: red">npm start</code> or
                        <code style="color: red">npm run webapp:build</code>.
                    </li>
                    <li>
                        You had a network error while running <code style="color: red">npm install</code>. If you are
                        behind a corporate proxy, it is
                        likely that this error was caused by your proxy. Have a look at the JHipster error logs, you
                        will probably have the cause of
                        the error.
                    </li>
                    <li>
                        You installed a Node.js version that doesn't work with JHipster: please use an LTS (long-term
                        support) version, as it's the
                        only version we support.
                    </li>
                </ol>
                <h2>Building the client side code again</h2>
                <p>If you want to go fast, run <code style="color: red">./mvnw</code> to build and run everything.</p>
                <p>If you want to have more control, so you can debug your issue more easily, you should follow the
                    following steps:</p>
                <ol>
                    <li>Install npm dependencies with the command <code style="color: red">npm install</code></li>
                    <li>
                        Build the client with the command <code style="color: red">npm run webapp:build</code> or
                        <code style="color: red">npm start</code>
                    </li>
                    <li>Start the server with <code style="color: red">./mvnw</code> or using your IDE</li>
                </ol>

                <h2>Getting more help</h2>

                <h3>If you have a question on how to use JHipster</h3>
                <p>
                    Go to Stack Overflow with the
                    <a href="http://stackoverflow.com/tags/jhipster" target="_blank"
                        rel="noopener noreferrer">"jhipster"</a> tag.
                </p>

                <h3>If you have a bug or a feature request</h3>
                <p>
                    First read our
                    <a href="https://github.com/jhipster/generator-jhipster/blob/main/CONTRIBUTING.md" target="_blank"
                        rel="noopener noreferrer">contributing guidelines</a>.
                </p>
                <p>
                    Then, fill a ticket on our
                    <a href="https://github.com/jhipster/generator-jhipster/issues/new/choose" target="_blank"
                        rel="noopener noreferrer">bug tracker</a>, we'll be happy to resolve your issue!
                </p>

                <h3>If you want to chat with contributors and other users</h3>
                <p>
                    Join our chat room on
                    <a href="https://gitter.im/jhipster/generator-jhipster" target="_blank"
                        rel="noopener noreferrer">Gitter.im</a>. Please note
                    that this is a public chat room, and that we expect you to respect other people and write in a
                    correct English language!
                </p>
                <!-- end of troubleshooting content -->
            </div>
        </div>
    </div>
    <noscript>
        <h1>You must enable JavaScript to view this page.</h1>
    </noscript>
    <script type="text/javascript">
        // show an error message if the app loading takes more than 4 sec
      window.onload = function () {
        setTimeout(showError, 4000);
      };
      function showError() {
        var errorElm = document.getElementById('jhipster-error');
        if (errorElm && errorElm.style) {
          errorElm.style.display = 'block';
        }
      }
    </script>
    <!-- uncomment this for adding service worker
    <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
          navigator.serviceWorker.register('/service-worker.js')
            .then(function () {
              console.log('Service Worker Registered');
            });
        });
      }
    </script>
    -->
    <!-- Google Analytics: uncomment and change UA-XXXXX-X to be your site's ID.
    <script>
      (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
      function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
      e=o.createElement(i);r=o.getElementsByTagName(i)[0];
      e.src='//www.google-analytics.com/analytics.js';
      r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
      ga('create','UA-XXXXX-X');ga('send','pageview');
    </script>-->
    <script defer src="app/vendors.bundle.js"></script>
    <script defer src="app/main.bundle.js"></script>
</body>

</html>

如果有人能帮助我理解为什么会发生这种情况和/或如何解决它,我将不胜感激。 先感谢您。

这是一个复合问题。 我在你的问题中发现了一些问题。

首先,您应该将路径更改为

@GetMapping(value = {"/users/{dateDu}", "/users/{dateDu}/{dateAu}"})

否则,如果您未在请求中提供dateAu ,您将收到 404 错误。


其次,您应该将@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)添加到您的参数中,以告诉Spring 如何将String 转换为LocalDate。 否则,您将收到带有错误消息的Bad Request

无法将“java.lang.String”类型的值转换为所需的“java.time.LocalDate”类型

我猜你没有注意到这个问题,因为 Swagger 已经为你处理过了。

我尝试修复您的代码:

@GetMapping(value = {"/users/{dateDu}", "/users/{dateDu}/{dateAu}"})
public ResponseEntity<List<User>> getAllUsersByDate(
    @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDu,
    @PathVariable(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Optional<LocalDate> dateAu,
    @org.springdoc.api.annotations.ParameterObject Pageable pageable) {
    if (dateAu.isEmpty()) {
        dateAu = Optional.of(LocalDate.now()); 
    }
    Page<User> page = userRepository.getUsersByDate(dateDu, dateAu.get(), pageable);
    HttpHeaders headers = PaginationUtil
        .generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
    return ResponseEntity.ok().headers(headers).body(page.getContent());
}

我认为如果您使用邮递员进行测试,它应该可以工作。


第三,根据这篇文章,所有@PathVariable必须在 Swagger 中是必需的。 这意味着您的问题与 Java 中@PathVariableOptional的组合无关。 这是 Swagger 的规范/限制。 因此,如果你真的想使用 Swagger 来测试你的 API,你有两个选择:

  1. 正如@tucuxi 所说,将其拆分为 2 个端点。
  2. 请改用 QueryString 样式。

最后,似乎您可能不需要Optional Optional被设计为在大多数情况下用作返回类型。 但这是一个有争议的设计问题。 所以,如果你真的想使用Optional ,你可以按照这篇优秀文章描述的最佳实践

也许尝试将 2 个路径添加到 @GetMapping?

@GetMapping("/users/{dateDu}", "/users/{dateDu}/{​​dateAu}")

不确定这是否能解决您的问题,但您可能应该删除参数注释中的错误标志: @PathVariable Optional<LocalDate> dateAu并添加两个路径: @GetMapping(value = {"/users/{dateDu}", "/users/{dateDu}/{dateAu}"})

暂无
暂无

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

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