LoginSignup
4
3

More than 5 years have passed since last update.

Jersey Client で JavaからAPI連携する

Last updated at Posted at 2019-02-19

必要なmaven

以下3つが必要

           <groupId>org.glassfish.jersey.core</groupId>
           <artifactId>jersey-client</artifactId>
           <version>2.27</version>

           <groupId>org.glassfish.jersey.media</groupId>
           <artifactId>jersey-media-json-jackson</artifactId>
           <version>2.26</version>

           <groupId>org.glassfish.jersey.inject</groupId>
           <artifactId>jersey-hk2</artifactId>
           <version>2.26</version>

足りないと

java.lang.ClassNotFoundException: org.glassfish.jersey.internal.l10n.LocalizableMessageFactory$ResourceBundleSupplier

こういうわけわかんないエラーでる

実装:POSTで連携する

✩http://localhost:8080/test/api1 のAPIを呼びたい

public void excute(RequestData req) {

        String result = ClientBuilder.newClient()
                // 連携先の決まってるurl
                .target("http://localhost:8080")
                // パス 連携先で変わるところ
                .path("/test/api1")
                //Xml形式でリクエストするかJSON形式でリクエストするか決めるところ   
                // xmlならこれを指定→APPLICATION_XML_TYPE
                .request(MediaType.APPLICATION_JSON_TYPE)
                // データ突っ込む
                //MediaTypeでなんの形式でレスポンス受け取るか指定今回はJSON
                .post(Entity.entity(req, MediaType.APPLICATION_JSON), String.class);

        // 標準出力にレスポンスを表示
        System.out.println(result);

    }
4
3
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
4
3