LoginSignup
1
1

More than 5 years have passed since last update.

@アノテーション

Last updated at Posted at 2018-10-15

@PathVariable
URLから値を取得し、変数に格納してくれる。
URL : http://localhost:8080/person/post/999

  @RequestMapping(value="/post/{id}", method=RequestMethod.POST)
  public Person getMessages(@PathVariable(value = "id", required = false) Integer id){
        Person person = new Person();
        person.setId(id);

        return person;
  }

結果 : {"id":999}

@PathVariable (複数の場合)
URL : http://localhost:8080/person/post/1/userName/99

  @RequestMapping(value="/post/{id}/{name}/{age}",method=RequestMethod.POST)
  public Person getMessages1(@PathVariable(value = "id", required = false) Integer id,@PathVariable(value = "name", required = false) String name,@PathVariable(value = "age", required = false) Integer age){
        Person mydata = new Person(id,name,age);
        //serviceは別で定義
        return service.saveAndFlush(mydata);

  }

結果 : {"id":1,"name":"userName","age":99}

@NoArgsConstructor
デフォルトコンストラクタを生成する

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class Man {

    private long id;

    private String name;

        //※下記のコンストラクタを自動生成してくれる。
        public Man() {
        }
}

@AllArgsConstructor
全メンバをセットするためのコンストラクタを生成する

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Man {

    private long id;

    private String name;

    //※下記の全メンバをセットしたコンストラクタを自動生成してくれる。
    public Man(final long id, final String name) {
        this.id = id;
        this.name = name;
    }

}
1
1
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
1
1