์์กด์ฑ ์ถ๊ฐํ๊ธฐ
pom.xml ์ ๋ค์ ์์กด์ฑ์ ์ถ๊ฐํ๋ค.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
์์กด์ฑ ์ถ๊ฐ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
Swagger ๊ด๋ จ ์ค์ ์ฝ๋ ์์ฑ
com.springboot.api.config.SwaggerConfiguration.java๋ฅผ ์์ฑํ๋ค.
package com.springboot.api.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfiguration {
@Bean
public InternalResourceViewResolver defaultViewResolver() {
return new InternalResourceViewResolver();
}
@Bean
public Docket api(){
// ์ค์บํ ํ์ด์ง ๋ฒ์ ์ค์ (ํด๋น ํจํค์ง ๋ฐ ํ์ ํจํค์ง๋ฅผ ์ค์บ)
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.springboot.api"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Spring Boot Open API Test with Swagger")
.description("์ค๋ช
๋ถ๋ถ")
.version("1.0.0")
.build();
}
}
์ ์ํ๊ธฐ
๋ค์ ์ฃผ์๋ก ์ ์ํ ํ๋ฉด์ด๋ค.
์๋ ์ฌ์ง๊ณผ ๊ฐ์ด ์ง์ GUIํ๊ฒฝ์์ ์คํํด ๋ณผ ์ ์๋ค.
API ์ค์
- ๋ค์๊ณผ ๊ฐ์ด ๋ฉ์๋์ ๋ํ ์ค๋ช
, ํ๋ผ๋ฏธํฐ์ ๋ํ ์ค๋ช
์ด ์๋ ์ํ์ด๋ค.
์ด๋ฅผ ์ถ๊ฐํด๋ณด์.
์
@GetMapping("/request1")
public String getRequestParam1(
@RequestParam String name,
@RequestParam String email,
@RequestParam String organization){
return name+" "+email+" "+organization;
}
ํ
@ApiOperation(value="GET ๋ฉ์๋",notes="@RequestParam์ ํ์ฉํ GET Method")
@GetMapping("/request1")
public String getRequestParam1(
@ApiParam(value="์ด๋ฆ",required = true) @RequestParam String name,
@ApiParam(value="์ด๋ฉ์ผ",required = true) @RequestParam String email,
@ApiParam(value="ํ์ฌ",required = true) @RequestParam String organization){
return name+" "+email+" "+organization;
}
๊ฒฐ๊ณผ
Swagger ๊ด๋ จ ์ค๋ฅ
Swagger๋ฅผ ์ ์ฉํ๋ฉด์ ๊ฒช์๋ ์ค๋ฅ๋ฅผ ์ ๋ฆฌํ๋ค.
์ค๋ฅ 1 : Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.24.jar:5.3.24]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.7.jar:2.7.7]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.7.jar:2.7.7]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.7.jar:2.7.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.7.jar:2.7.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.7.jar:2.7.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.7.jar:2.7.7]
at com.springboot.api.ApiApplication.main(ApiApplication.java:11) ~[classes/:na]
Caused by: java.lang.NullPointerException: null
at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]
at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]
at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[na:na]
at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
at java.base/java.util.TimSort.sort(TimSort.java:220) ~[na:na]
at java.base/java.util.Arrays.sort(Arrays.java:1515) ~[na:na]
at java.base/java.util.ArrayList.sort(ArrayList.java:1750) ~[na:na]
at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:na]
at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) ~[springfox-spring-web-3.0.0.jar:3.0.0]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.24.jar:5.3.24]
... 14 common frames omitted
Process finished with exit code 1
- ์์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฌ๋ค๋ฉด
ApiApplication.java
์ @EnableWebMvcAnnotation
์ ์ถ๊ฐํ๋ค.
package com.springboot.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
์ค๋ฅ 2: Whitelabel Error Page
- ๋ค์ ์ฃผ์๋ก ์ ๊ทผ์ ํ ๋ ํ์ด์ง๊ฐ ์ ๋๋ก ํ์๋์ง ์๋ ๊ฒฝ์ฐ
- ๋ค์ @Bean ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
@Configuration
public class SwaggerConfiguration {
@Bean
public InternalResourceViewResolver defaultViewResolver() {
return new InternalResourceViewResolver();
}
Uploaded by N2T
'Programming > [Spring Boot]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JpaRepository getById, findById (0) | 2023.03.12 |
---|---|
DB ๊ด๋ จ ์ฉ์ด (1) | 2023.02.01 |
PUT/DELETE API ๋ง๋ค๊ธฐ (1) | 2023.01.21 |
POST API ๋ง๋ค๊ธฐ (0) | 2023.01.20 |
GET API ๋ง๋ค๊ธฐ (0) | 2023.01.19 |