かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

Spring BootでJSONを返すAPIを作る

超簡単でした。@RestControllerで戻り値がオブジェクトでOKっぽいです。

package okazuki.igniteui.controllers;

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

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/json")
public class HomeController {
    @RequestMapping
    public List<Person> jsonSample() {
        List<Person> result = new ArrayList<Person>();
        for (int i = 0; i < 100; i++) {
            result.add(new Person("tanaka" + i, i));
        }
        return result;
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}