m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring BootでXMLやJSONを返す

忙しいです…

(´・ω・`)

Spring BootでRest APIを作成する場合には

・コントローラー用のクラスに"@RestController"アノテーションを付ける
・クラスを任意で用意して返却することで基本、json形式で返却できる
XMLを返したい場合は返却用のクラスに"@XmlRootElement"を付与する必要があります

返却用のクラス

Item.java
package com.example.entity;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Item implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String date;
	
	public Item(){
	}
	public Item(String name, String date){
		this.name = name;
		this.date = date;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
	
}

コントローラー

Sample1Controller.java
package com.example.web;

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

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 com.example.entity.Item;

@RestController
@RequestMapping("/sample1")
public class Sample1Controller {
	
	private List<Item> items = new ArrayList<Item>();
	
	Sample1Controller(){
		items.add(new Item("hoeg1", "20131122"));
		items.add(new Item("hoeg2", "20131130"));
		items.add(new Item("hoeg3", "20131205"));
		items.add(new Item("hoeg4", "20131210"));
		items.add(new Item("hoeg5", "20131225"));
		items.add(new Item("hoeg6", "20141012"));
		items.add(new Item("hoeg7", "20141014"));
		items.add(new Item("hoeg8", "20141024"));
	}
	
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public String index(){
		return "/sample/index";
	}
	
	@RequestMapping(value = "/test1", method = { RequestMethod.GET })
	public String test1(){
		return "/sample/test1";
	}
	
	@RequestMapping(value = "/items", method = { RequestMethod.GET })
	public List<Item> items(){
		return this.items;
	}
	
	@RequestMapping(value = "/item/{id}", method = { RequestMethod.GET })
	public Item item(@PathVariable int id){
		return this.items.get(id);
	}
	
}

内容

・/sample1
→ "/sample/index"を返す

$ curl -i -H "Accept: application/json" \
>        http://localhost:8080/sample1/
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Content-Length: 13
Date: Sat, 17 Dec 2016 04:10:47 GMT

/sample/index

・/sample1/test1
→ "/sample/test1"を返す

$ curl -i -H "Accept: application/json" \
>        http://localhost:8080/sample1/test1
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Content-Length: 13
Date: Sat, 17 Dec 2016 04:11:29 GMT

/sample/test1

・/sample1/items
→ itemクラスのjson一覧を返す

$ curl -i -H "Accept: application/json" \
>        http://localhost:8080/sample1/items
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 17 Dec 2016 04:11:56 GMT

[{"name":"hoeg1","date":"20131122"},{"name":"hoeg2","date":"20131130"},{"name":"hoeg3","date":"20131205"},{"name":"hoeg4","date":"20131210"},{"name":"hoeg5","date":"20131225"},{"name":"hoeg6","date":"20141012"},{"name":"hoeg7","date":"20141014"},{"name":"hoeg8","date":"20141024"}]

・/sample1/item{id}
→ itemクラスのjsonを返す

$ curl -i -H "Accept: application/json" \
>        http://localhost:8080/sample1/item/5
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 17 Dec 2016 04:12:21 GMT

{"name":"hoeg6","date":"20141012"}

所感

XMLは工夫が必要ですが基本的にはjsonで返す。
json時は特に何もしなくてもクラスを返すだけでいいので楽そう。