gpt4 book ai didi

java - RestTemplate postForObject 无法映射除 String.class 之外的自定义类

转载 作者:行者123 更新时间:2023-12-02 00:57:45 25 4
gpt4 key购买 nike

我试图使用 RestTemplate postForObject 使用 firebase Auth API,但是当我将 String.class 指定为其工作文件的返回类型时,但如果我指定自定义类来处理响应,则失败,尽管自定义类属性与JSON 返回,

restTemplate.postForObject( uri, request, UserResponse.class);
public class UserResponse {

private String kind;
private String localId;
private String email;
private String displayName;
private String idToken;
private Boolean registered;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getLocalId() {
return localId;
}

public void setLocalId(String localId) {
this.localId = localId;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getIdToken() {
return idToken;
}

public void setIdToken(String idToken) {
this.idToken = idToken;
}

public Boolean getRegistered() {
return registered;
}

public void setRegistered(Boolean registered) {
this.registered = registered;
}
}

谁能指出我错了;

异常(exception):

2020-04-09 16:06:06.980 ERROR 18200 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at aaa.Application.main(Application.java:10) ~[classes/:na]
Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class aaa.model.UserResponse] and content type [application/json;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:123) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:418) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at aaa.config.Bootstrap.run(Bootstrap.java:49) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
... 5 common frames omitted

最佳答案

添加jackson依赖项。

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>

关于java - RestTemplate postForObject 无法映射除 String.class 之外的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61119116/

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