Skip to content

Commit

Permalink
feat(java11/swagger): separate annotations with interface example
Browse files Browse the repository at this point in the history
  • Loading branch information
bugoverdose committed Mar 27, 2024
1 parent 4635295 commit c60e529
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
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());
}
}
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 등)
}
}
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 {
}
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();
}
}
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);
}

0 comments on commit c60e529

Please sign in to comment.