🗂

SpringBootでRESTfulWebサービスの利用

2021/12/31に公開

https://spring.io/guides/gs/consuming-rest/
このチュートリアルを参考にして、RestAPIを呼び出す処理を実装する
このサンプルに出てくるサンプルのRestApiは何故か応答がない。
ダミーのRestAPIを無料で使用できるサービスがあるので、それを利用する
https://jsonplaceholder.typicode.com/

サンプルAPI
https://jsonplaceholder.typicode.com/users/1

Result
{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}
RestserviceApplication.java
package com.example.restservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RestserviceApplication {

	private static final Logger log =
		LoggerFactory.getLogger(RestserviceApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(RestserviceApplication.class, args);
	}

	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder){
		return builder.build();
	}

	@Bean
	public CommandLineRunner run (RestTemplate restTemplate) throws Exception{
		return args -> {
			log.info("START");
			User user = restTemplate.getForObject(
					"https://jsonplaceholder.typicode.com/users/1",User.class);
			log.info(user.toString());
			log.info("END");
		};
	}
}
Address.java
package com.example.restservice;

// import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Data;

// @JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class Address {

  private String zipcode;
  private String city;
  private String street;
}
User.java
package com.example.restservice;

// import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
// import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import lombok.Data;
// @JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class User {
  private String id;
  private String name;
  private String email;
  private Address address;
  private String  phone;
  private String  xxxxx;
}
@JsonIgnoreProperties(ignoreUnknown = true)

未知の項目があった場合に無視する
これをしないと未知の項目があった場合に
org.codehaus.jackson.map.exc.UnrecognizedPropertyException
が発生するのはずだが、これがある、なし、true、flaseにかかわらず、
未知の項目があった場合にもエラーにならなかった
org.codehaus.jackson.annotate.JsonIgnoreProperties
com.fasterxml.jackson.annotation.JsonIgnoreProperties
の両方試したがダメだった。

STDOUT
2021-12-31 21:21:06.978  INFO 74566 --- [           main] c.e.restservice.RestserviceApplication   : START
2021-12-31 21:21:07.544  INFO 74566 --- [           main] c.e.restservice.RestserviceApplication   : User(id=1, name=Leanne Graham, email=Sincere@april.biz, address=Address(zipcode=92998-3874, city=Gwenborough, street=Kulas Light), phone=1-770-736-8031 x56442, xxxxx=null)
2021-12-31 21:21:07.544  INFO 74566 --- [           main] c.e.restservice.RestserviceApplication   : END

Discussion