Api 弹簧休息模板-为什么我得到;白标错误“;?
嗨,我想制作一个非常简单的应用程序,在控制台中打印openweathermap.org API答案。我认为它应该可以工作,但当我要在我的浏览器中的地址,我有白标签错误页面,在控制台中什么也没有。我上了两节课。主要内容:Api 弹簧休息模板-为什么我得到;白标错误“;?,api,spring-mvc,resttemplate,Api,Spring Mvc,Resttemplate,嗨,我想制作一个非常简单的应用程序,在控制台中打印openweathermap.org API答案。我认为它应该可以工作,但当我要在我的浏览器中的地址,我有白标签错误页面,在控制台中什么也没有。我上了两节课。主要内容: package com.example.restTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringAppl
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();
}
}
和控制器:
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());
}
}
package com.example.restemplate.Controller;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpMethod;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.client.rest模板;
导入java.util.array;
@RestController
公共类Web服务{
@自动连线
rest模板rest模板;
@请求映射(value=“localhost:8080/weather”)
公众天气(){
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity=新的HttpEntity(标题);
System.out.println(restemplate.exchange(“http://api.openweathermap.org/data/2.5/weather?q=London,英国和myAPIKey“,
HttpMethod.GET,entity,String.class).getBody();
}
}
当然,我没有把API的真正密钥放在这里,我应该声明city并在params中输入密钥,但我以后会考虑它。现在,我只想在控制台中获得一个简单的输出,非常感谢您的帮助。为什么要将“localhost:8080/weather”
作为值
从
@RequestMapping(value ="localhost:8080/weather")
到
你说得对。我也不知道。非常感谢。:)
@RequestMapping(value = "/weather")