Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Commit

Permalink
RESTコントローラを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
akihyro committed Nov 22, 2014
1 parent cce7b98 commit 59235d3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
</parent>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/akihyro/tryspringboot/hoges/HogeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package akihyro.tryspringboot.hoges;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import lombok.val;

/**
* Hoge コントローラ。
*/
@RestController
@RequestMapping("/hoges")
public class HogeController {

/**
* データストア。
*/
private static List<HogeData> store = new ArrayList<HogeData>();

/**
* データ作成。
*
* @param data データ。
* @return レスポンス。
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<HogeData> post(@ModelAttribute HogeData data, UriComponentsBuilder uriComponentsBuilder) {
store.add(data);
val headers = new HttpHeaders();
headers.setLocation(uriComponentsBuilder.path("/hoges/{id}").buildAndExpand(store.size()).toUri());
return new ResponseEntity<HogeData>(data, headers, HttpStatus.CREATED);
}

/**
* データリスト取得。
*
* @return データリスト。
*/
@RequestMapping(method = RequestMethod.GET)
public List<HogeData> get() {
return store;
}

/**
* データ取得。
*
* @param id データのID。
* @return データ。
*/
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public HogeData get(@PathVariable int id) {
return store.get(id - 1);
}

}
22 changes: 22 additions & 0 deletions src/main/java/akihyro/tryspringboot/hoges/HogeData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package akihyro.tryspringboot.hoges;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import lombok.Data;

@Data
@XmlRootElement
public class HogeData {

/** 数値。 */
private Integer integer;

/** 文字列。 */
private String string;

/** 文字列リスト。 */
private List<String> strings;

}

0 comments on commit 59235d3

Please sign in to comment.