代码之家  ›  专栏  ›  技术社区  ›  Dean Huang

使用restful web服务:控制器springboot错误

  •  2
  • Dean Huang  · 技术社区  · 6 年前

    我正在研究如何在Spring中使用REST控制器,我使用Eclipse来运行Java/Spring,我想问一下,在运行Java时,我得到了这样的错误。我已经试图寻找原因,等仍然没有找到解决办法。有人知道怎么修吗?

    Exception in thread "main" org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.fasterxml.jackson.annotation.JsonIgnoreProperties$Value]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.fasterxml.jackson.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.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:238)
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.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.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.fasterxml.jackson.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.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
        at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
        at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
        at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3072)
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:235)
        ... 6 more
    

    我的应用程序

    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());
        }
    
    }
    

    引用Java:

    package hello;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.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 +
                    '}';
        }
    }
    

    价值爪哇

    package hello;
    
    import com.fasterxml.jackson.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:

    <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.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>
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Ankit Rastogi    6 年前

    问题就在这条线上

    package hello;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    
    // Below you are importing wrong Value class
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties.Value;
    

    您正在导入 com.fasterxml.jackson.annotation.jsonignoreproperties.value 而不是你的 你好,价值 班在 爪哇 文件

    取代你 爪哇 用下面的代码归档。

    package hello;
    
    import com.fasterxml.jackson.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 +
                    '}';
        }
    }
    
        2
  •  1
  •   Rafał Sokalski    6 年前

    问题是spring无法正确创建对象,因为类中没有匹配的构造函数

    向类中添加构造函数:

    爪哇价值

    public Value(Long id, String quote) {
     this.id = id;
     this.quote = quote;
    }
    

    爪哇

    public Quote(String type, Value value) {
     this.type= type;
     this.value= value;
    }
    

    上次我遇到同样的问题: Invoke REST controller method returns 404 null