[JAVA] Try hitting the zip code search API with Spring Boot

What is the Zip Code Search API?

API published by Japan Post. You can get the zip code using REST.

Development environment

This time, we are running in the following environment.

This time's leading role: Rest Template

Implementation

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<>();

I will actually move it

thus… image.png

like this! image.png

Recommended Posts

Try hitting the zip code search API with Spring Boot
03. I sent a request from Spring Boot to the zip code search API
Try using the Rails API (zip code)
Try using Spring Boot with VS Code
[API] I tried using the zip code search API
Spring Boot programming with VS Code
I made a simple search form with Spring Boot + GitHub Search API.
Create a simple search app with Spring Boot
Try LDAP authentication with Spring Security (Spring Boot) + OpenLDAP
Try to implement login function with Spring Boot
Try to automate migration with Spring Boot Flyway
Create Spring Boot environment with Windows + VS Code
Create a web api server with spring boot
Download with Spring Boot
Try using DI container with Laravel and Spring Boot
Automatically map DTOs to entities with Spring Boot API
Hello World (REST API) with Apache Camel + Spring Boot 2
Try using OpenID Connect with Keycloak (Spring Boot application)
Access the built-in h2db of spring boot with jdbcTemplate
[Spring Boot] Get user information with Rest API (beginner)
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
Try Dependency Inversion Principle with Multiple Spring Boot Projects
Customize REST API error response with Spring Boot (Part 1)
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Change the injection target for each environment with Spring Boot 2
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Get started with Spring boot
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Hello World with Spring Boot!
A story packed with the basics of Spring Boot (solved)
Run LIFF with Spring Boot
SNS login with Spring Boot
Let's make a simple API with EC2 + RDS + Spring boot ①
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Try using Spring Boot Security
Implement REST API with Spring Boot and JPA (domain layer)
Try Spring Boot on Mac
Try the Spring WebFlux tutorial
Create microservices with Spring Boot
Send email with spring boot
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Implement a simple Web REST API server with Spring Boot + MySQL
Sample code that uses the Mustache template engine in Spring Boot
See the relative redirect behavior with the server.tomcat.use-relative-redirects setting in Spring Boot
Part 1: Try using OAuth 2.0 Login supported by Spring Security 5 with Spring Boot
Sample code to unit test a Spring Boot controller with MockMvc
A story about hitting the League Of Legends API with JAVA
See the behavior of entity update with Spring Boot + Spring Data JPA
Sample code to call the Yahoo! Local Search API in Java
[Beginner] Let's write REST API of Todo application with Spring Boot
Use Basic Authentication with Spring Boot
Spring with Kotorin --4 REST API design