Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

Spring BootでJSONを返すAPIを作る

$
0
0

超簡単でした。@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")
publicclass HomeController {
    @RequestMappingpublic 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;
    privateint age;

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

    public String getName() {
        return name;
    }

    publicvoid setName(String name) {
        this.name = name;
    }

    publicint getAge() {
        return age;
    }

    publicvoid setAge(int age) {
        this.age = age;
    }

}

Viewing all articles
Browse latest Browse all 1387

Trending Articles