java 如何修复Sping Boot 应用程序中的类型定义错误?

ujv3wf0j  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(102)

我正在构建一个Sping Boot 应用程序,它利用了com.explore.explore.entities.Spot类。但是得到了这个错误
Type definition error: [simple type, class com.explore.explore.entities.Spot]] with root cause
我已经检查了Spot类及其依赖项,但似乎找不到问题。如何修复此错误并将Spot对象正确序列化为JSON?
我已经尝试了以下步骤:

  • 已检查Spot类是否定义正确,所有字段和方法是否注解正确。
  • 已检查Spot类中使用的任何自定义数据类型是否定义正确且注解正确。
  • 已检查Spot类使用的任何依赖项是否存在并正确配置。
    MyController.java
package com.explore.explore.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.explore.explore.entities.Spot;
import com.explore.explore.services.SpotService;

@RestController
public class MyController {

    @Autowired
    public SpotService SpotService;

    @GetMapping("/hello")
    public String home() {
        return "hello";
    }

    // get spots
    @GetMapping("/spots")
    // public List<Integer> getSpots() {
    // List<Integer> list;
    // list = new ArrayList<>();
    // list.add(3);
    // list.add(5);
    // return this.SpotService.getSpots();
    // }

    public List<Spot> getSpots() {
        
        List<Spot> list;
        list = new ArrayList<>();
        list.add(new Spot(1, "38 Block", "Near 38"));
        list.add(new Spot(0, "UNIMAll", "Near Me"));
        return list;
    }
}

Spot.java

package com.explore.explore.entities;

import org.springframework.http.converter.HttpMessageConversionException;

public class Spot {
    private long id;
    private String name;
    private String location;

    public Spot(long id, String name, String location) {
    
        super();
        this.id = id;
        this.name = name;
        this.location = location;
    
    }
    public Spot() {
        super();
    }
}
noj0wjuj

noj0wjuj1#

我需要为序列化添加注解
@JsonAutoDetect@JsonProperty@JsonInclude

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Spot {
    @JsonProperty("id")
    private long id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("location")
    private String location;

    public Spot(long id, String name, String location) {
        super();
        this.id = id;
        this.name = name;
        this.location = location;
    }

    public Spot() {
        super();
    }

    // getters and setters here
}
92dk7w1h

92dk7w1h2#

你用 Spring Boot 吗?如果你用Spring Boot....
您可以添加@getter

@Getter
public class Spot {
    private long id;
    private String name;
    private String location;

    public Spot(long id, String name, String location) {

        super();
        this.id = id;
        this.name = name;
        this.location = location;

    }
    public Spot() {
        super();
    }
}

相关问题