I am getting a Nullpointer Exception when calling a service with the help of WebClient.
Service
@Service
public class DummyWebclient {
@Autowired
private WebClient getWebClient;
public DummyWebclient() {}
Response client3 = getWebClient.get().
uri(uriBuilder -> uriBuilder
.queryParam("param1", "value1")
.queryParam("param2", "value2")
.build() )
.retrieve()
.bodyToMono(Response.class)
.block();
public Response getClient3() {
return client3;
}
}
Template Webservice
@Component
public class GetWebClient {
@Bean
public WebClient GetWebclient() {
return WebClient
.builder()
.baseUrl(MainURL.getMAINURL())
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build()
;
}
}
And a simple test to reveive the response throws NullPointer Exception, Caused by: java.lang.NullPointerException at (DummyWebclient.java:19)
Line Response client3 = getWebClient.get().
@ExtendWith(SpringExtension.class)
@SpringBootTest
class DummyWebclientTest{
@Autowired
private DummyWebclient dummyWebclient;
@Test
public void GetResponse2() {
Response response2 = dummyWebclient.getClient3();
System.out.println(response2);
}
}
My question is: why does Spring does not create a Bean and insert to Service?