Java 使用 RESTful Web 服务:控制器 Springboot 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50873799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consuming a RESTful Web Service: Controller Springboot error
提问by Dean Huang
i'm studying how to consume rest controller in spring, i'm using eclipse to run the java/spring , I want to ask i got error like this when running the java. i already tried to search about the caused,etc still dont find the solutions. anyone know how to fix it?
我正在研究如何在 spring 中使用 rest 控制器,我正在使用 eclipse 来运行 java/spring ,我想问一下我在运行 java 时遇到了这样的错误。我已经尝试搜索原因等仍然没有找到解决方案。谁知道怎么修它?
Exception in thread "main" org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.fasterxml.Hymanson.annotation.JsonIgnoreProperties$Value]; nested exception is com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.fasterxml.Hymanson.annotation.JsonIgnoreProperties$Value` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 28] (through reference chain: hello.Quote["value"])
at org.springframework.http.converter.json.AbstractHymanson2HttpMessageConverter.readJavaType(AbstractHymanson2HttpMessageConverter.java:238)
at org.springframework.http.converter.json.AbstractHymanson2HttpMessageConverter.read(AbstractHymanson2HttpMessageConverter.java:223)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:332)
at hello.Application.main(Application.java:15)
Caused by: com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.fasterxml.Hymanson.annotation.JsonIgnoreProperties$Value` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 28] (through reference chain: hello.Quote["value"])
at com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.Hymanson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
at com.fasterxml.Hymanson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.Hymanson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:3072)
at org.springframework.http.converter.json.AbstractHymanson2HttpMessageConverter.readJavaType(AbstractHymanson2HttpMessageConverter.java:235)
... 6 more
My Application Java
我的应用程序 Java
package hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
}
}
Quote Java:
引用 Java:
package hello;
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties.Value;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
public Quote() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
@Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}
}
Value Java
价值Java
package hello;
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
public Value() {
}
public Long getId() {
return this.id;
}
public String getQuote() {
return this.quote;
}
public void setId(Long id) {
this.id = id;
}
public void setQuote(String quote) {
this.quote = quote;
}
@Override
public String toString() {
return "Value{" +
"id=" + id +
", quote='" + quote + '\'' +
'}';
}
}
Pom XML :
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.resttemplate</groupId>
<artifactId>hello1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hello1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.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.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
采纳答案by Ankit Rastogi
The problem is in this line
问题出在这一行
package hello;
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
// Below you are importing wrong Value class
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties.Value;
You are importing com.fasterxml.Hymanson.annotation.JsonIgnoreProperties.Valueinstead of your hello.Valueclass in Quote.javafile
您正在导入com.fasterxml.Hymanson.annotation.JsonIgnoreProperties.Value而不是Quote.java文件中的hello.Value类
Replace your Quote.javafile with the below code.
将您的Quote.java文件替换为以下代码。
package hello;
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
import hello.Value;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
public Quote() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
@Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}
}
回答by Rafa? Sokalski
The problem is that Spring can't create properly object because you don't have matching constructor in your classess
问题是 Spring 无法正确创建对象,因为您的类中没有匹配的构造函数
Add constructors to your classes:
将构造函数添加到您的类中:
Value.java
值.java
public Value(Long id, String quote) {
this.id = id;
this.quote = quote;
}
Quote.java
报价.java
public Quote(String type, Value value) {
this.type= type;
this.value= value;
}
Last time I had the same problem: Invoke REST controller method returns 404 null
上次我遇到了同样的问题: 调用 REST 控制器方法返回 404 null