Spring Core/자질구래

-parameter 옵션

  • -

이번 포스트에서는 java compiler 옵션 중 -parameter 옵션에 대해 알아보자!

 

-parameter 옵션

 

문제!

가끔 다음과 같은 컨트롤러를 만들어서 동작시키면 안될 때가 있다. 

@GetMapping("/posts/{id}")
public Post getPostById(@PathVariable Long id) {  // @PathVariable(name="id")
    return service.getPostById(id);
}

그러다 name 속성을 추가하면 잘 된다. 사실 name 속성은 전달된 이름과 사용할 파라미터가 다를 때 지정하는데 여기서는 같아서 사실 문제가 되지는 않는데 이상한 일이다. 왜일까?

 

파라미터가 매핑되는 과정

-parameters는 Java Compiler의 옵션 이다.

  javac 
  -parameters
        Generate metadata for reflection on method parameters

name이 지정된 경우는 바로 해당 파라미터와 연결하지만 name이 없을 경우 Spring은 reflection으로 파라미터 이름을 알아낸다. 이를 위해서는 컴파일 과정에서 위와 같이 parameters 옵션이 켜져 있어야 한다.

 

-parameters 설정

 

build tools

maven

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <parameters>true</parameters>
    </configuration>
</plugin>

gradle

tasks.withType(JavaCompile) {
    options.compilerArgs << '-parameters'
}

 

tool

intellij

IntelliJ 설정:
Settings → 
Build → 
Compiler → 
Java Compiler → "Additional command line parameters"에 -parameters 추가

eclipse(STS)

preferences > java > compiler > Store information about method parameters check!

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.