在 dropwizard 中为 REST webservice 配置 URI 前缀

Configuring URI prefix for REST webservice in dropwizard

我正在使用 dropwizard 开发 REST API。可以使用 https://<host>:port/item/1 访问 resource。可以看出没有 URI 前缀。如果我必须配置一个 URI 前缀需要做什么。可以在yaml配置文件中配置吗? 谢谢!

是的,可以在 YAML 中配置 URI 前缀 a.k.a 根路径。您可以使用简单的服务器工厂配置。很简单,在 YAML 中添加这两行。我使用 'api' 作为前缀。您可以将其替换为您想要的 URI 前缀。

server:
  rootPath: '/api/*'

稍微复杂一点的服务器配置看起来像这样,

server:
  adminConnectors:
    -
      port: 18001
      type: http
  adminContextPath: /admin
  applicationConnectors:
    -
      port: 18000
      type: http
  rootPath: /api/*
  type: default

您可以参考此示例https://github.com/dropwizard/dropwizard/blob/master/dropwizard-example/example.yml了解服务器和其他配置详情。

如果您刚开始使用 dropwizard,完成此步骤也是一个好主意 http://www.dropwizard.io/0.9.2/docs/getting-started.html