在dropwizard中为REST webservice配置URI前缀

在dropwizard中为REST webservice配置URI前缀

问题描述:

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

是的URI前缀a.k.a根路径可以在YAML中配置。您可以使用简单的服务器工厂配置。很简单,在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