-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.java
56 lines (48 loc) · 1.45 KB
/
api.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.external_api;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
@RestController
public class api {
@GetMapping(value = "/address")
public Result search(@RequestParam("zipcode") String zipcode) {
RestTemplate restTemplate = new RestTemplate();
String json = restTemplate.getForObject("https://zipcloud.ibsnet.co.jp/api/search?zipcode=" + zipcode,
String.class);
// 文字列を変換してJSON(Result)にする
ObjectMapper mapper = new ObjectMapper();
Result result = null;
try {
result = mapper.readValue(json, Result.class);
} catch (JsonProcessingException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return result;
}
@Getter
@Setter
public static class Result {
private int status;
private String message;
private List<Results> results;
@Getter
@Setter
public static class Results {
private String zipcode;
private String prefcode;
private String address1;
private String address2;
private String address3;
private String kana1;
private String kana2;
private String kana3;
}
}
}