我得到了上面提到的错误:
控制器:
@Component
public class NiftyController {
@Autowired
private RestProxyTemplate restTemplate;
@Autowired
private NiftyDAO niftyDAO;
int count=1;
@Scheduled(fixedDelay=5000)
//@Scheduled(cron="*/5 * * * * ?")
public void loadNiftyData() throws URISyntaxException {
System.out.println("Method executed at every 5 seconds. Current time is : "+ new Date() );
System.out.println("Executed " +count++ +" Times");
URI uri = new URI("https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json");
ResponseEntity<NiftyDTO> niftyResponse = restTemplate.getRestTemplate().exchange(uri, HttpMethod.GET, null, NiftyDTO.class);
NiftyDTO nifties = niftyResponse.getBody();
System.out.println(nifties.getTrdVolumesum());
//List<Nifty> saveNifty = niftyDAO.save(nifties);
//return (Nifty) nifties;
}
}
我想从上面的链接中获取数据并保存在数据库中
DTO课程
@JsonIgnoreProperties({"latestData","data"})
public class NiftyDTO {
private float trdVolumesumMil;
private String time;
private int declines;
private float trdValueSum;
private float trdValueSumMil;
private int unchanged;
private float trdVolumesum;
private int advances;
public float getTrdVolumesumMil() {
return trdVolumesumMil;
}
public void setTrdVolumesumMil(float trdVolumesumMil) {
this.trdVolumesumMil = trdVolumesumMil;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getDeclines() {
return declines;
}
public void setDeclines(int declines) {
this.declines = declines;
}
public float getTrdValueSum() {
return trdValueSum;
}
public void setTrdValueSum(float trdValueSum) {
this.trdValueSum = trdValueSum;
}
public float getTrdValueSumMil() {
return trdValueSumMil;
}
public void setTrdValueSumMil(float trdValueSumMil) {
this.trdValueSumMil = trdValueSumMil;
}
public int getUnchanged() {
return unchanged;
}
public void setUnchanged(int unchanged) {
this.unchanged = unchanged;
}
public float getTrdVolumesum() {
return trdVolumesum;
}
public void setTrdVolumesum(float trdVolumesum) {
this.trdVolumesum = trdVolumesum;
}
public int getAdvances() {
return advances;
}
public void setAdvances(int advances) {
this.advances = advances;
}
}
RestProxyTemplate:
@Component
public final class RestProxyTemplate {
RestTemplate restTemplate;
@PostConstruct
public void init(){
restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("MY PROXY GOES HERE",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
}
错误日志:
Method executed at every 5 seconds. Current time is : Mon Jun 05 10:45:38 IST 2017
Executed 1 Times
2017-06-05 10:45:38.141 INFO 108988 --- [ main]
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
2017-06-05 10:45:38.153 INFO 108988 --- [ main]
com.MyApplication.portfolio.PMSApplication : Started PMSApplication in 5.041 seconds (JVM running for 5.573)
2017-06-05 10:45:39.028 ERROR 108988 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.MyApplication.portfolio.controller.NiftyDTO] and content type [text/plain]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:493) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at com.MyApplication.portfolio.controller.NiftyController.loadNiftyData(NiftyController.java:44) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_92]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_92]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_92]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_92]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_92]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_92]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
我找到了响应类型的No Sutiable HttpMessageConverter,有人会建议可能的解决方案吗?
答案 0 :(得分:0)
您需要处理application / json而不是text / plain。
#include <stdio.h>
int main(void)
{
int arr[11] = { 1, 1, 2, 3, 4, 5, 4, 7, 8, 9, 8 };
int col[11];
int i;
int j;
int k;
int ctr = 0;
for (i = 0; i < 11; i++)
{
for (j = i + 1; j < 10; j++)
{
if (arr[i] == arr[j])
{
i++;
}
}
printf("%d", arr[i]);
}
getch();
return 0;
}
代码来自the docs并稍加修改
更新:更改代码正常工作但有另一个错误导致:private static void temp() {
HttpHeaders requestHeaders = new HttpHeaders();
// requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
requestHeaders.setAccept(Collections.singletonList(new MediaType("text","plain")));
HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
MappingJackson2HttpMessageConverter mc = new MappingJackson2HttpMessageConverter() ;
mc.setSupportedMediaTypes(Collections.singletonList(new MediaType("text","plain")));
restTemplate.getMessageConverters().add(mc);
String url = "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json";
ResponseEntity<NiftyDTO> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, NiftyDTO.class);
NiftyDTO res = responseEntity.getBody();
System.out.println(res);
}
您需要使用com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type float from String "4,092.10": not a valid Float value
分隔符修复数据的反序列化。