-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java11/swagger): separate annotations with interface example
- Loading branch information
1 parent
4635295
commit c60e529
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
java11/swagger/src/main/java/example/bugoverdose/swagger/auth/ArgumentResolverConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package example.bugoverdose.swagger.auth; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class ArgumentResolverConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(new AuthArgumentResolver()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
java11/swagger/src/main/java/example/bugoverdose/swagger/auth/AuthArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package example.bugoverdose.swagger.auth; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
public class AuthArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
// 매개변수에 @AuthUserId 어노테이션이 붙어있을 때 | ||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(AuthUserId.class); | ||
} | ||
|
||
// 해당 매개변수의 인자로 아래 로직의 반환값을 넣어준다 | ||
@Override | ||
public Object resolveArgument(MethodParameter parameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
WebDataBinderFactory binderFactory) { | ||
return 1L; // 요청 메시지에 담긴 정보를 활용하여 가공된 정보 (ex. JWT 토큰을 통해 알아낸 유저의 id 등) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
java11/swagger/src/main/java/example/bugoverdose/swagger/auth/AuthUserId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package example.bugoverdose.swagger.auth; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AuthUserId { | ||
} |
23 changes: 23 additions & 0 deletions
23
java11/swagger/src/main/java/example/bugoverdose/swagger/presentation/ExampleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package example.bugoverdose.swagger.presentation; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import example.bugoverdose.swagger.auth.AuthUserId; | ||
|
||
@RestController | ||
@RequestMapping("/api/examples") | ||
public class ExampleController implements SwaggerExampleController { | ||
|
||
@GetMapping("/{exampleId}") | ||
public ResponseEntity<Void> getExample(@AuthUserId Long resolvedArgument, | ||
@PathVariable Long exampleId, | ||
@RequestParam Boolean isExample) { | ||
// 생략 | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...gger/src/main/java/example/bugoverdose/swagger/presentation/SwaggerExampleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package example.bugoverdose.swagger.presentation; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import springfox.documentation.annotations.ApiIgnore; | ||
|
||
@Api(tags = {"예시 API"}) | ||
public interface SwaggerExampleController { | ||
|
||
@ApiOperation(value = "예시를 조회한다") | ||
ResponseEntity<Void> getExample(@ApiIgnore Long resolvedArgument, | ||
Long exampleId, | ||
Boolean isExample); | ||
} |