API published by Japan Post. You can get the zip code using REST.
This time, we are running in the following environment.
Service Class
ZipCodeService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ZipCodeService {
	@Autowired
	@Qualifier("zipCodeSearchRestTemplate")
	RestTemplate restTemplate;
	/**Zip Code Search API Request URL*/
	private static final String URL = "http://zipcloud.ibsnet.co.jp/api/search?zipcode={zipcode}";
	public ZipCodeDto service(String zipcode) {
		return restTemplate.getForObject(URL, ZipCodeDto.class, zipcode);
	}
}
What is actually hitting the API is
restTemplate.getForObject
It is the part of.
Dto Receive the data obtained by hitting with RestTemplate as a Java object.
ZipCodeDto.java
package com.taku.springboot;
import java.util.ArrayList;
import java.util.List;
public class ZipCodeDto {
	/**status*/
	int status;
	/**message*/
	String message;
	/**Postal code information list*/
	List<ZipCodeDataDto> results = new ArrayList<>();
thus…

like this!

Recommended Posts