26
26

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 Client メモ - REST API を呼び出してレスポンスをBeanにマッピングする

Posted at

やりたいこと

  • REST APIを呼び出してレスポンス(XML or JSON)をBeanにマッピングする

前提条件

  • JAX-RSのRI(Reference Implementation)にはJersey 2.xを使用
  • 下記のようなユーザ検索サービスを想定

GET /webresources/users/1

XMLの場合
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
  <id>1</id>
  <name>John</name>
  <age>25</age>
</user>
JSONの場合
{
  "id":1,
  "name":"John",
  "age":25
}

GET /webresources/users

XMLの場合
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<users>
  <user>
    <id>1</id>
    <name>John</name>
    <age>25</age>
  </user>
  <user>
    <id>2</id>
    <name>Paul</name>
    <age>27</age>
  </user>
  <user>
    <id>3</id>
    <name>George</name>
    <age>28</age>
  </user>
  <user>
    <id>4</id>
    <name>Ringo</name>
    <age>26</age>
  </user>
</users>
JSONの場合
[
{
  "id":1,
  "name":"John",
  "age":25
},
{
  "id":2,
  "name":"Paul",
  "age":27
},
{
  "id":3,
  "name":"George",
  "age":28
},
{
  "id":4,
  "name":"Ringo",
  "age":26
}
]

実装してみる

1. 依存関係の解決

  • 下記のように依存性を定義する(Gradleの場合)
build.gradle
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.+'

// JSONから変換する場合は以下を定義する
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.+'

2. コード例

GET /webresources/user/1

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/WebApplication1/webresources")
    .path("users/{id}");

User user = target.resolveTemplate("id", 1)
    .request(MediaType.APPLICATION_XML)
    .get(User.class);

System.out.println(user);
// => User(id=1, name=John, age=25)
User.java
@lombok.Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class User {

    private Integer id;
    private String name;
    private Integer age;

}

GET /webresources/users

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/WebApplication1/webresources")
    .path("/users");

List<User> users = target.request(MediaType.APPLICATION_XML).get(new GenericType<List<User>>(){});

System.out.println(users);
// => [User(id=1, name=John, age=25), User(id=2, name=Paul, age=27), User(id=3, name=George, age=28), User(id=4, name=Ringo, age=26)]

JSONから変換したい

  • ClientConfigとJacksonFeatureを使用する
ClientConfig config = new ClientConfig().register(JacksonFeature.class);
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target("http://localhost:8080/WebApplication1/webresources")
    .path("users/{id}");

User user = target.resolveTemplate("id", 1)
    .request(MediaType.APPLICATION_JSON)
    .get(User.class);

System.out.println(user);
// => User(id=1, name=John, age=25)

こんな感じです。

26
26
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
26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?