gpt4 book ai didi

java - 读取 Spring RestTemplate 调用超时

转载 作者:行者123 更新时间:2023-12-05 06:12:13 25 4
gpt4 key购买 nike

我有一个 API,它返回一个类型为 GET 方法的 json。由于它是 GET,当我在浏览器中打开 URL 时它工作正常并呈现 json,但是,在使用 RestTemplate 检索 json 时失败。

能否请您提供一种阅读以下 API 的方法。

API 网址:https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY

Spring Boot Rest 模板调用:

final String uri = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY";
RestTemplate restTemplate = new RestTemplate();
Map result = restTemplate.getForObject(uri, Map.class);

错误:

java.net.SocketTimeoutException: Read timed out
at java.base/java.net.SocketInputStream.socketRead0(Native Method) ~[na:na]
at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115) ~[na:na]
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168) ~[na:na]
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140) ~[na:na]
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448) ~[na:na]
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823) ~[na:na]
at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:252) ~[na:na]
at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:292) ~[na:na]
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:351) ~[na:na]
at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:746) ~[na:na]
at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:689) ~[na:na]

最佳答案

您可以改用 webflux 中的 WebClient:

- 添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

- 创建 POJO

@Data
public class Root {
private Records records;
private Filtered filtered;

@Data
public static class PE {
private int strikePrice;
private String expiryDate;
private String underlying;
private String identifier;
private int openInterest;
private int changeinOpenInterest;
private double pchangeinOpenInterest;
private int totalTradedVolume;
private double impliedVolatility;
private double lastPrice;
private double change;
private double pChange;
private int totalBuyQuantity;
private int totalSellQuantity;
private int bidQty;
private double bidprice;
private int askQty;
private double askPrice;
private double underlyingValue;
}

@Data
public static class CE {
private int strikePrice;
private String expiryDate;
private String underlying;
private String identifier;
private int openInterest;
private int changeinOpenInterest;
private int pchangeinOpenInterest;
private int totalTradedVolume;
private int impliedVolatility;
private int lastPrice;
private double change;
private double pChange;
private int totalBuyQuantity;
private int totalSellQuantity;
private int bidQty;
private double bidprice;
private int askQty;
private double askPrice;
private double underlyingValue;
}

@Data
public static class Datum {
private int strikePrice;
private String expiryDate;
private PE PE;
private CE CE;
}

@Data
public static class Records {
private List<String> expiryDates;
private List<Datum> data;
private String timestamp;
private double underlyingValue;
private List<Integer> strikePrices;
}

@Data
public static class Filtered {
//TODO
}
}

- 让 WebClient 进行调用

@SpringBootApplication
public class MultipleConfigurationPropertiesApplication {

public static void main(String[] args) {
SpringApplication.run(MultipleConfigurationPropertiesApplication.class, args);
}

@Bean
CommandLineRunner commandLineRunner() {
return args -> {
WebClient client = WebClient.builder()
.baseUrl("https://www.nseindia.com")
.exchangeStrategies(ExchangeStrategies.builder()
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(16 * 1024 * 1024))
.build())
.build();

Mono<Root> result = client.get()
.uri("/api/option-chain-indices?symbol=NIFTY").accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Root.class);

System.out.println(result.block());
};
}
}

由于请求结果很大,您必须调整缓冲区大小。

此外,您可能需要考虑使用非阻塞/流解决方案来处理此问题。

关于java - 读取 Spring RestTemplate 调用超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com