Last active
November 7, 2018 17:05
-
-
Save keiyonekawa0614/628ed396b78497e55bad0508a287e80e to your computer and use it in GitHub Desktop.
簡単!Spring BootでWebAPIをたたいてJSON形式のレスポンスをjavaオブジェクトに変換 ref: http://qiita.com/yk0614/items/c306bddc66ab7882c7ba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile("com.fasterxml.jackson.core:jackson-databind") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class RestTemplateResolver { | |
@Bean | |
public RestTemplate zipCodeSearchRestTemplate() { | |
RestTemplate restTemplate = new RestTemplate(); | |
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); | |
List<MediaType> supportedMediaTypes = new ArrayList<>(messageConverter.getSupportedMediaTypes()); | |
supportedMediaTypes.add(MediaType.TEXT_PLAIN); // text/plainのJacksonの処理対象にくわえる | |
messageConverter.setSupportedMediaTypes(supportedMediaTypes); | |
restTemplate.setMessageConverters(Collections.singletonList(messageConverter)); // カスタムしたHttpMessageConverterを適用 | |
return restTemplate; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
th:each="item : ${zipcodeList}" th:object="${item}" | |
th:text="*{zipcode}" | |
th:text="*{prefcode}" | |
th:text="*{address1}" | |
・ | |
・(省略) | |
・ | |
th:text="*{kana3}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class ZipCodeController { | |
@Autowired | |
ZipCodeService zpcService; | |
/** | |
* 郵便番号入力フォーム | |
* @return "zipcode" | |
*/ | |
@RequestMapping("/zipcode") | |
public String zipcodeForm(HttpSession session, Model model) { | |
return "zipcode"; | |
} | |
/** | |
* 郵便番号情報表示 | |
* @return "zipcode-confirm" | |
*/ | |
@RequestMapping(value="/zipcode/confirm", method=RequestMethod.POST) | |
public String zipcodeConfirm(HttpSession session, Model model, | |
@RequestParam("zipcode") String zipcode){ | |
// 一応必須チェックのみ 数字・桁数チェックは省略 | |
// nullまたは空文字の場合、入力フォームにエラーメッセージを表示 | |
if (zipcode == null || zipcode.equals("")) { | |
model.addAttribute("errorMessage", "郵便番号を入力してください。"); | |
return zipcodeForm(session, model); | |
} | |
// 郵便番号検索APIサービス呼び出し | |
ZipCodeDto zipCodeDto = zpcService.service(zipcode); | |
// thymeleafでリストを展開して表示する | |
model.addAttribute("zipcodeList", zipCodeDto.getResults()); | |
return "zipcode-confirm"; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 郵便番号 */ | |
String zipcode; | |
/** 都道府県コード */ | |
String prefcode; | |
/** 都道府県名 */ | |
String address1; | |
/** 市区町村名 */ | |
String address2; | |
/** 町域名 */ | |
String address3; | |
/** 都道府県名カナ */ | |
String kana1; | |
/** 市区町村名カナ */ | |
String kana2; | |
/** 町域名カナ */ | |
String kana3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ステータス */ | |
int status; | |
/** メッセージ */ | |
String message; | |
/** 郵便番号情報リスト */ | |
List<ZipCodeDataDto> results = new ArrayList<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class ZipCodeService { | |
@Autowired | |
@Qualifier("zipCodeSearchRestTemplate") | |
RestTemplate restTemplate; | |
/** 郵便番号検索API リクエスト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); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment