gpt4 book ai didi

spring - 如何在 Spring-Web 中使用 RestTemplate 解析 gzip 编码的响应

转载 作者:IT老高 更新时间:2023-10-28 13:47:47 24 4
gpt4 key购买 nike

我修改后Consuming a RESTful Web Service调用 get users by id 的示例从 api.stackexchange.com 我得到 JsonParseException:

com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): 标记之间只允许使用常规空格 (\r,\n,\t)

来自 api.stackexchange.com 的响应是 gzip 压缩的。

如何在 Spring-Web RestTemplate 中添加对 gzip 压缩响应的支持?

我正在使用 Spring Boot 父版本。 1.3.1.RELEASE 因此 Spring-Web 4.2.4-RELEASE

这是我调整后的示例:

用户.java

package stackexchange.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class User {

// Properties made public in order to shorten the example
public int userId;
public String displayName;
public int reputation;

@Override
public String toString() {
return "user{"
+ "display_name='" + displayName + '\''
+ "reputation='" + reputation + '\''
+ "user_id='" + userId + '\''
+ '}';
}
}

CommonWrapper.java

package stackexchange.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class CommonWrapper {

// Properties made public in order to shorten the example
public boolean hasMore;
// an array of the type found in type
public User[] items;
public int page;
public int pageSize;
public int quotaMax;
public int quotaRemaining;

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (User user : items) {
sb.append("{" + user.toString() + "}\n");
}

return "common_wrapper{"
+ "\"items\"=[\n"
+ sb
+ "]"
+ "has_more='" + hasMore + '\''
+ "page='" + page + '\''
+ "page_size='" + pageSize + '\''
+ "quota_max='" + quotaMax + '\''
+ "quota_remaining='" + quotaRemaining + '\''
+ '}';
}
}

StackExchange.java

package stackexchange;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.web.client.RestTemplate;

import stackexchange.dto.CommonWrapper;

import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class StackExchange implements CommandLineRunner{

private static final Logger log = LoggerFactory.getLogger(StackExchange.class);

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

@Override
public void run(String... strings) throws Exception {

RestTemplate restTemplate = new RestTemplate();
CommonWrapper response = restTemplate
.getForObject(
"https://api.stackexchange.com/2.2/users/4607349?site=stackoverflow",
CommonWrapper.class);

log.info(response.toString());
}

}

pom.xml - 与示例相同

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>stackexchangetest</groupId>
<artifactId>stackexchangetest</artifactId>
<version>0.0.1</version>
<name>stackexchangetest</name>
<description>api.stackexchange.com Test</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

最佳答案

将默认 requestFactory 替换为 Apache HttpClient 中的一个(动态解码 GZIP):

HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

在 pom.xml 中添加 Apache Http Client

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!--Version is not needed when used with Spring Boot parent pom file -->
<version>4.5.1</version>
</dependency>

关于spring - 如何在 Spring-Web 中使用 RestTemplate 解析 gzip 编码的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34415144/

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