This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
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.
- Loading branch information
akihyro
committed
Nov 22, 2014
1 parent
cce7b98
commit 59235d3
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
src/main/java/akihyro/tryspringboot/hoges/HogeController.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,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); | ||
} | ||
|
||
} |
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,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; | ||
|
||
} |