如何使用属性文件设置 jersey 和 swagger-ui 以设置 url 变量?
How to set up jersey and swagger-ui with properties file for setting url variable?
我有一个使用 Jersey 作为 REST API 的 Maven spring 应用程序。对于要将代码部署到的每个环境,我都有一个属性文件。
例如,dev.properties
看起来像:
hostUrl=https://dev.foo.net/
basePathUrl=/bar/restapi
而且test.properties看起来像
hostUrl=https://test.foo.net/
basePathUrl=/bar/restapi
然后我使用 spring beans 为每个环境配置 swagger:
<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
<property name="title" value="Swagger App"/>
<property name="version" value="1.0.0" />
<property name="schemes" value="http" />
<property name="host" value="#{envSpecificProperties.hostUrl}" />
<property name="basePath" value="#{envSpecificProperties.basePathUrl}"/>
<property name="resourcePackage" value="com.foo.bar.rest"/>
<property name="scan" value="true"/>
</bean>
<context:property-placeholder
properties-ref="envSpecificProperties" />
<util:properties id="envSpecificProperties"
location="WEB-INF/classes/file-#{xjpEnvironment.domain}.properties" />
<xjp:environment />
我的问题是,如何使用这些相同的属性文件在 index.html
中设置 swagger-ui
,这样 swagger-ui
就不必像这样硬编码[=21] =]
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://localhost:8080/foo/bar/restapi/swagger.json";
}
并且可以通过属性文件以某种方式动态设置,例如:
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "#{beanConfig.host}" + "#{beanConfig.basePath}" + "swagger.json";
}
请告诉我maven swagger jersey和spring是否可行。
谢谢!
您可以添加 Bootstrap servlet 并在其中使用属性文件中的值设置 bean 配置。
更多详情请参考:
https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 and
要动态设置 index.html 中的值,请使用 JavaScript 函数构成 url。像这样:
url = "http://" + window.location.host + window.location.pathname + "api/swagger.json";
我有一个使用 Jersey 作为 REST API 的 Maven spring 应用程序。对于要将代码部署到的每个环境,我都有一个属性文件。
例如,dev.properties
看起来像:
hostUrl=https://dev.foo.net/
basePathUrl=/bar/restapi
而且test.properties看起来像
hostUrl=https://test.foo.net/
basePathUrl=/bar/restapi
然后我使用 spring beans 为每个环境配置 swagger:
<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
<property name="title" value="Swagger App"/>
<property name="version" value="1.0.0" />
<property name="schemes" value="http" />
<property name="host" value="#{envSpecificProperties.hostUrl}" />
<property name="basePath" value="#{envSpecificProperties.basePathUrl}"/>
<property name="resourcePackage" value="com.foo.bar.rest"/>
<property name="scan" value="true"/>
</bean>
<context:property-placeholder
properties-ref="envSpecificProperties" />
<util:properties id="envSpecificProperties"
location="WEB-INF/classes/file-#{xjpEnvironment.domain}.properties" />
<xjp:environment />
我的问题是,如何使用这些相同的属性文件在 index.html
中设置 swagger-ui
,这样 swagger-ui
就不必像这样硬编码[=21] =]
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://localhost:8080/foo/bar/restapi/swagger.json";
}
并且可以通过属性文件以某种方式动态设置,例如:
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "#{beanConfig.host}" + "#{beanConfig.basePath}" + "swagger.json";
}
请告诉我maven swagger jersey和spring是否可行。
谢谢!
您可以添加 Bootstrap servlet 并在其中使用属性文件中的值设置 bean 配置。
更多详情请参考:
https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 and
要动态设置 index.html 中的值,请使用 JavaScript 函数构成 url。像这样:
url = "http://" + window.location.host + window.location.pathname + "api/swagger.json";