目次
はじめに
今回は、リクエストされたデータをkeyにDB更新するRest API(PUT)をSpring Bootで作成します。
今回やることを絵にするとこんな感じ
環境
- OS:OS X 10.11 El Capitan
- STS:3.8.4.RELEASE
前提
以下が完了している前提で説明します。
【Spring Boot入門(4)】Rest API(GET)を作ってみる
Rest API(PUT)の作成
RestAPI(PUT)は以下の手順で作成します。
- サービス・クラス作成(ItemService.java)修正
- コントローラ(ItemController.java)修正
1.サービス・クラス作成(ItemService.java)修正
src/main/java/com/example/service/ItemService.java
package com.example.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.Item;
import com.example.repository.ItemRepository;
@Service
@Transactional
public class ItemService {
@Autowired
ItemRepository itemRepository;
/**
* 商品一覧取得サービス
* @return List<item>
*/
public List<Item> findAll() {
return itemRepository.findAll();
}
/**
* 商品登録サービス
* @param item
* @return item
*/
public Item create(Item item) {
return itemRepository.save(item);
}
/**
* 商品削除サービス
* @param id
*/
public void delete(Integer id) {
itemRepository.delete(id);
}
/**
* 商品更新サービス
* @param item
* @return item
*/
public Item update(Item item) {
return itemRepository.save(item);
}
}
2.コントローラ(ItemController.java)修正
src/main/java/com/example/api/ItemRestController.java
以上でRestAPI(PUT)の修正は完了です。package com.example.api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.Item;
import com.example.service.ItemService;
@RestController
@RequestMapping("api/items")
public class ItemRestController {
@Autowired
ItemService itemService;
/**
* 商品一覧取得API
* @return List<item>
*/
@GetMapping
List<Item> getItems() {
List<Item> customers = itemService.findAll();
return customers;
}
/**
* 商品登録API
* @param item
* @return item
*/
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
Item postItem(@RequestBody Item item) {
return itemService.create(item);
}
/**
* 商品削除API
* @param id
*/
@DeleteMapping(path = "{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
void deleteItem(@PathVariable Integer id) {
itemService.delete(id);
}
/**
* 商品更新API
* @param id
* @param item
* @return item
*/
@PutMapping(path = "{id}")
Item putItem(@PathVariable Integer id, @RequestBody Item item) {
item.setId(id);
return itemService.update(item);
}
}
RestAPI(PUT)の動作確認
パッケージ・エクスプローラーでsrc/main/java/com/example/RestApiApplication.javaを右クリック>実行>Spring Boot アプリケーションでAPサーバを起動します。
Google Chromeで「http://localhost:8080/api/items」にアクセスします。
ターミナルを開き、curlコマンドでRestAPIをコールします。
command
再度、Google Chromeで「http://localhost:8080/api/items」にアクセスします。$ curl http://localhost:8080/api/items/4 -i -XPUT -H "Content-Type: application/json" -d "{\"name\":\"もも\",\"price\":\"1000\",\"imgPath\":\"peach.jpg\"}"
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 02 Jul 2017 01:45:52 GMT
{"id":4,"name":"もも","price":1000,"imgPath":"peach.jpg"}
id=4のぶどうがももに変更されたことが確認できます。
以上で、Spring BootによるRestAPI(PUT)の作成は完了です。
おわりに
今回はSpring BootでRest API(PUT)を作成する手順について解説しました。
Spring BootでのRest APIの作成はREST API(GET)、REST API(POST)
と同様に非常に簡単でした。
今回まででRest APIのGET、POST、PUT、DELETEが作成できました。
あんまり面白いサンプルではありませんでしたね。。。
これらをフロントエンドのアプリ(Angularで作成したアプリ)からコールして動かしたりすると少しは面白くなるかも、、、
コメントを残す