質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

1回答

506閲覧

Spring Boot RestTemplate

kwiyy

総合スコア12

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

0クリップ

投稿2019/11/28 06:44

SpringbootでbitflyerのAPIを叩いて情報を取得してきたいんですが
ぬるぽが取れません。原因がわからずです。
参考サイト

Java

1package com.example.demo; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.beans.factory.annotation.Qualifier; 5import org.springframework.stereotype.Service; 6import org.springframework.web.client.RestTemplate; 7 8@Service 9public class ProductService { 10 11 @Autowired 12 @Qualifier("ProductsRestTemplate") 13 RestTemplate restTemplate; 14 15 private static final String URL = "https://api.bitflyer.com/v1/markets"; 16 17 public ProductDto service() { 18 19 ProductDto dto = restTemplate.getForObject(URL, ProductDto.class); 20 System.out.println(dto.product_type); 21 return dto; 22 23 } 24}

Java

1package com.example.demo; 2 3import java.util.ArrayList; 4import java.util.Collections; 5import java.util.List; 6 7import org.springframework.context.annotation.Bean; 8import org.springframework.http.MediaType; 9import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 10import org.springframework.stereotype.Component; 11import org.springframework.web.client.RestTemplate; 12 13@Component 14public class RestTemplateResolver { 15 16 @Bean 17 public RestTemplate ProductsRestTemplate() { 18 RestTemplate restTemplate = new RestTemplate(); 19 MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); 20 List<MediaType> supportedMediaTypes = new ArrayList<>(messageConverter.getSupportedMediaTypes()); 21 supportedMediaTypes.add(MediaType.APPLICATION_JSON); // text/plainのJacksonの処理対象にくわえる 22 messageConverter.setSupportedMediaTypes(supportedMediaTypes); 23 restTemplate.setMessageConverters(Collections.singletonList(messageConverter)); // カスタムしたHttpMessageConverterを適用 24 return restTemplate; 25 } 26}
Exception in thread "main" java.lang.NullPointerException at com.example.demo.ProductService.service(ProductService.java:21) at com.example.demo.RequestController.getProducts(RequestController.java:22) at com.example.demo.RestPracticeApplication.main(RestPracticeApplication.java:11)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

おそらくControllerクラスからProductServiceの呼び出し方が誤っているからでしょう。(この質問には当該クラスが書かれていないので予想でしかありませんが)

少なくとも以下のようにすればServiceクラスからComponentで定義されたRestTemplateは取得できます。(ProductDtoの実装如何では、その後JSONの変換エラーが出るかもしれませんね)

java

1package com.example.demo.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.web.bind.annotation.GetMapping; 5import org.springframework.web.bind.annotation.RequestMapping; 6import org.springframework.web.bind.annotation.RestController; 7 8import com.example.demo.service.RestSampleService; 9import com.example.demo.service.RestSampleService.ProductDto; 10 11@RestController 12@RequestMapping("/rest") 13public class RestSampleController { 14 15 @Autowired 16 ProductService service; 17 18 @GetMapping("") 19 public ProductDto sample() { 20 return service.service(); 21 } 22} 23

投稿2019/11/28 12:41

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問