Hi i want to make very simple application that print an openweathermap.org API answer in console. I thought it should works, but when i'm going to an adress in my browser i've got Whitelabel Error Page and nothing in console. I've made two classes. Main:
package com.example.restTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(RestTemplateApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
and controller:
package com.example.restTemplate.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "localhost:8080/weather")
public void printWeather() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
System.out.println(restTemplate.exchange("http://api.openweathermap.org/data/2.5/weather?q=London,uk&myAPIKey",
HttpMethod.GET, entity, String.class).getBody());
}
}
Of course i didn't put here my real key to API and i should to declared city and key in params, but i will think about it in future. For now i just want to get a simple output in console and wll be appreciated for your help.
Why do you put "localhost:8080/weather"
as value?
Change from
@RequestMapping(value ="localhost:8080/weather")
To
@RequestMapping(value = "/weather")