我试图获取数据,但总是让403(Forbidden)与RestTemplate.
但是当我尝试时org.apache.http.client.HttpClient,一切都很好.我也可以在我的机器上使用Postman获取数据.
代码很简单但我不知道什么是错的.
public Object get() {
try {
RestTemplate restTemplate = new RestTemplate();
Object result = restTemplate.getForObject("https://api.hearthstonejson.com/v1/19776/enUS/cards.json", Object.class);
return result;
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:附加堆栈跟踪
org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
at com.brawlstone.metaservice.service.SyncService.get(SyncService.java:49)
at com.brawlstone.metaservice.web.SyncController.getCards(SyncController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) …Run Code Online (Sandbox Code Playgroud) 我是thymeleaf和springboot的新手.
我使用IntelliJ IDEA创建了一个带有百万美元的springboot项目,其中包含"New Project" - >"Spring Initalizr".
然后我得到一个项目,其结构如下所示.
demo
|+-src/main
| +-java
| +-resources
| static
| templates
| application.properties
|-pom.xml
Run Code Online (Sandbox Code Playgroud)
问题是我应该在资源下放置静态和模板?他们之间有什么区别?
我想具有百万美元属性的.html文件应该放在模板中.静态html,css和js怎么样?
谢谢.
我是SpringBoot的新手。
我基于Springboot的示例项目来创建我的项目。我想仅针对js / css文件控制用于缓存的http标头。
我在下面添加了一个js文件test.js,src/resources/static然后在中引用它greeting.html。
然后,我在StackOverflow上遵循一些答案,并添加WebMvcConfiguration.java和WebSecurityConfig.java,但它对我不起作用。
我确定WebMvcConfiguration和WebSecurityConfig不应一起使用,或者我配置有误。我更喜欢使用Java Config而不是XML的解决方案。
我打开ChromeDevTool来检查服务器的响应。响应http标头是
Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 10:53:14 GMT
Expires:
Last-Modified:Thu, 02 Feb 2017 10:52:49 GMT
Pragma:
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:当我删除时WebSecurityConfig,我得到以下标题
Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 11:14:51 GMT
Last-Modified:Thu, 02 Feb 2017 11:14:20 GMT
Run Code Online (Sandbox Code Playgroud)
greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script th:src="@{/test.js}"></script>
</head> …Run Code Online (Sandbox Code Playgroud) 我想根据环境中的当前文化自动显示日期时间.
例如,如果我DateTime.ToShortDateString()用来转换Datetime"2013年5月4日",它将4/5/2013在en-us和5/4/2013in中en-gb.
但我要展示的是04/05/2013en-us和05/04/2013en-gb.零无法隐藏.
请注意,该软件可能在不同的文化中运行,而不仅仅是在美国和英国.我不想为每种文化设置日期时间格式.