英文:
RestTemplate postForObject unable to map Custom class apart from String.class
问题
我尝试使用RestTemplate的postForObject方法来消费Firebase Auth API,但是当我将返回类型指定为String.class时,它能正常工作;但是当我将自定义的类指定为返回类型来处理响应时,它失败了,尽管自定义类的属性与JSON返回的属性相同。
```java
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;
}
}
有人可以告诉我我错在哪里吗?
异常信息:
2020-04-09 16:06:06.980 ERROR 18200 --- [ main] o.s.boot.SpringApplication : 应用运行失败
java.lang.IllegalStateException: 执行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: 无法提取响应:无适用的HttpMessageConverter可用于响应类型 [class aaa.model.UserResponse] 和内容类型 [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
英文:
I was trying to consume firebase Auth API using RestTemplate postForObject, but when I assign String.class as return type its working file but if I assign my custom class to handle the response its failed, although custom class property are same as the JSON return,
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;
}
}
Can any one where I am wrong;
Exceptions:
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
答案1
得分: 1
添加 jackson
依赖。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
英文:
Add jackson
dependency.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论