10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAX-RSでJSONを返す

Last updated at Posted at 2016-07-06

JAX-RSでJSONを返すのは簡単。JSONをサポートする拡張モジュールを導入してアノテーションで返却するメディアタイプをapplication/jsonを指定すれば良い。拡張モジュールがJSONに変換できるオブジェクトだったら面倒見てくれる。

build.gradle
dependencies {
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.23.1'

}

JerseyにはいくつかのJSON変換モジュールが用意されているがこれはJacksonを使ったモジュール。

リソースクラスのメソッドに@Producesを使ってメディアタイプを指定する。

JsonResource.java
package example.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/")
public class JsonResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String index() {
        return "Hello World";
    }

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> list() {
        return Arrays.asList("a","b");
    }

    @GET
    @Path("map")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> map() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "000");
        map.put("b", "111");
        return map;
    }

    static public class Person{
        public String name;
        public int age;
    };
    @GET
    @Path("pojo")
    @Produces(MediaType.APPLICATION_JSON)
    public Person pojo() {
        Person person = new Person();
        person.name = "Foo Bar";
        person.age = 30;
        return person;
    }
}

プリミティブ・リスト・マップ・POJOなら適当に面倒見てくれる。テストコードを見てどのように変換されるか確認。JSONの確認にはGroovy側のJsonSlurperを使った。

JsonResourceTest.groovy
package example.jaxrs

import groovy.json.JsonSlurper
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.JerseyTest
import spock.lang.Shared
import spock.lang.Specification

import javax.ws.rs.core.Application

class JsonResourceTest extends Specification {

    def jsonslurper = new JsonSlurper()

    @Shared
    def jerseyTest = new JerseyTest() {
        @Override
        protected Application configure() {
            return new ResourceConfig(JsonResource.class)
        }
    }

    def setupSpec() {
        jerseyTest.setUp()
    }

    def cleanupSpec() {
        jerseyTest.tearDown()
    }

    def "test index"() {
        when:
        def response = jerseyTest.target("/").request().get(String.class)
        then:
        response == "Hello World"
    }

    def "test list"() {
        when:
        def response = jerseyTest.target("/list").request().get(String.class)
        then:
        jsonslurper.parseText(response) == jsonslurper.parseText('''["a", "b"]''')

    }

    def "test map"() {
        when:
        def response = jerseyTest.target("/map").request().get(String.class)
        then:
        jsonslurper.parseText(response) == jsonslurper.parseText('''{"b": "111", "a": "000" }''')
    }

    def "test pojo"() {
        when:
        def response = jerseyTest.target("/pojo").request().get(String.class)
        then:
        jsonslurper.parseText(response) == jsonslurper.parseText('''{"name": "Foo Bar", "age": 30 }''')
    }
}
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?