需要更新Spring Boot项目的Swagger UI路径。

huangapple go评论82阅读模式
英文:

Need to update swagger UI Path for spring boot project

问题

我有一个Spring Boot项目,需要更新我的Swagger路径从

http://localhost:9012/swagger-ui.html

http://localhost:9012/myservice/swagger-ui.html

我没有为我的应用程序使用任何上下文路径,我尝试了下面的所有方法,但都没有起作用。

https://stackoverflow.com/questions/46180375/change-location-to-call-swagger-ui-in-spring

https://github.com/springfox/springfox/issues/1443

https://stackoverflow.com/questions/57193286/how-to-change-swagger-ui-html-default-path

https://github.com/springfox/springfox/issues/1080

https://stackoverflow.com/questions/50712519/how-to-change-swagger-ui-url-prefix

private static final String PATH = "/myservice";

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    final String apiDocs = "/v2/api-docs";
    final String configUi = "/swagger-resources/configuration/ui";
    final String configSecurity = "/swagger-resources/configuration/security";
    final String resources = "/swagger-resources";
    final String swaggerUI = "/swagger-ui.html";

    registry.addRedirectViewController(apiDocs, PATH + apiDocs).setKeepQueryParams(true);
    registry.addRedirectViewController(resources, PATH + resources);
    registry.addRedirectViewController(configUi, PATH + configUi);
    registry.addRedirectViewController(configSecurity, PATH + configSecurity);
    registry.addRedirectViewController(swaggerUI, PATH + swaggerUI);
    registry.addRedirectViewController("/", PATH);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(PATH + "/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry.addResourceHandler(PATH + "/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

我使用的Swagger版本是2.9.2
是否有其他解决此问题的方法。

英文:

I am having a spring boot project and need to update my swagger path from

> http://localhost:9012/swagger-ui.html

to

> http://localhost:9012/myservice/swagger-ui.html

I am not using any context path for my application I have tried all the below approach but none of them worked.

https://stackoverflow.com/questions/46180375/change-location-to-call-swagger-ui-in-spring

https://github.com/springfox/springfox/issues/1443

https://stackoverflow.com/questions/57193286/how-to-change-swagger-ui-html-default-path

https://github.com/springfox/springfox/issues/1080

https://stackoverflow.com/questions/50712519/how-to-change-swagger-ui-url-prefix

private static final String PATH = "/myservice";

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    final String apiDocs = "/v2/api-docs";
    final String configUi = "/swagger-resources/configuration/ui";
    final String configSecurity = "/swagger-resources/configuration/security";
    final String resources = "/swagger-resources";
    final String swaggerUI = "/swagger-ui.html";

    registry.addRedirectViewController(apiDocs, PATH +apiDocs).setKeepQueryParams(true);
    registry.addRedirectViewController(resources, PATH +resources);
    registry.addRedirectViewController(configUi, PATH +configUi);
    registry.addRedirectViewController(configSecurity, PATH +configSecurity);
    registry.addRedirectViewController(swaggerUI, PATH + swaggerUI);
    registry.addRedirectViewController("/",PATH);
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler(PATH + "/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry.addResourceHandler(PATH + "/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

I am using swagger version 2.9.2
Is there any other way to resolve the issue.

答案1

得分: 2

尝试在您的控制器中使用此重定向方法,我已经测试过它可以工作

@RequestMapping("/myservice/swagger-ui.html")
public String redirectToSwagger() {
    return "redirect:/swagger-ui.html";
}

根据您的需求,可以使用重定向或转发中的任何一个。

英文:

Try this redirect method in your controller, I have tested it and it works

@RequestMapping("/myservice/swagger-ui.html")
public String redirectToSwagger() {
	return "redirect:/swagger-ui.html";
}

You can use either of redirect or forward based on your requirement.

答案2

得分: 1

请使用以下的Swagger依赖项。您只需在pom.xml中添加它,它将像魔法一样工作。移除任何与Swagger相关的注解,比如 @EnableSwagger2

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

Swagger URL: http://{base_url}/swagger-ui/index.html

英文:

Please use the following swagger dependency. All you have to do is just add in the pom.xml, it will work like charm. Remove any swagger related annotation like @EnableSwagger2.

&lt;dependency&gt;
    &lt;groupId&gt;io.springfox&lt;/groupId&gt;
    &lt;artifactId&gt;springfox-boot-starter&lt;/artifactId&gt;
    &lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;

Swagger URL: http://{base_url}/swagger-ui/index.html

答案3

得分: 1

你可以尝试下面的代码,它需要使用 `@Controller` 而不是 `@RestController`
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
public class SwaggerMappingController {

	@RequestMapping("/swagger-ui.html")
	public String greeting() {
		return "redirect:/swagger-ui/index.html";
	}
}

<details>
<summary>英文:</summary>

You can try below code it requires `@Controller` not a `@RestController`

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
public class SwaggerMappingController {

@RequestMapping(&quot;/swagger-ui.html&quot;)
public String greeting() {
	return &quot;redirect:/swagger-ui/index.html&quot;;
}

}


</details>



huangapple
  • 本文由 发表于 2020年7月22日 13:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定