0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Foxで作ったAPI仕様書にコンテキストパスつけたかったけどついてた

Last updated at Posted at 2020-01-29

リファレンスにあったからやってみたけどやり方違ったって話

環境

Spring Boot 2.2.3.RELEASE - 2.2.4.RELEASE
Spring Fox 2.9.2
Kotlin 1.3.61
Gradle

環境は以下の記事のまんま

pathMapping使えばええやんって思ってた

コンテキストパスはapplication.propertiesで指定してるんですよ

application.properties
server.servlet.context-path=/test

@Valueで拾ってきたプロパティをpathMappingに入れればOK

SwaggerConfig.kt

@Configuration
@EnableSwagger2
class SwaggerConfig {

    @Value("\${server.servlet.context-path:/}")
    private val contextPath : String? = null

    @Bean
    fun configurateApiDocument(): Docket = Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .pathMapping(contextPath)
            .
            .
            .

application.propertiesからパラメータをとってくるときは@ConfigurationPropertiesを使ったほうがいいっぽいけど、
パラメータ1つしかとってこないから@Valueでも問題ないかと

pathMappingnullが入ってくるとまずいから、@Valueのデフォルト値に/を指定しとく

image.png

いいね

... と思っていたんだけどダメっぽい

誤解していたところ

server.servlet.context-pathの記載があれば、
実は特に何も設定しなくてもコンテキストパスの反映はやってくれてる

よく見るとちゃんとBaseUrlにはコンテキストパスがついてる

image.png

テスト用のURLが、それぞれのAPIにもコンテキストパスをつけたせいで2つかぶったURLになっちゃってる
image.png

っていうこともできるよってくらいにしといたほうがいいかも

pathMappingは何に使うんだろう・・・

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?