Spring Boot为什么会返回一些值为null的json?

GioPoe:

我正在做一个练习游戏。

我有一个连接到MySQL数据库的spring boot / Maven项目。我能够设置一个仅从我的“ allriddles”表中检索所有内容的api。它曾经工作过,但现在api返回一个json,其中某些键的某些值为null。我唯一正在玩的是spring boot文件上的application.properties。我在spring.jpa.hibernate.ddl-auto的“更新”和“无”之间进行切换。我做错了什么?

application.properties

spring.jpa.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/riddlesgame
spring.datasource.username=root
spring.datasource.password=RiddlesGame886
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.data.rest.base-path=/api/v1
server.port=8088

这是数据库表和预期的列

数据库表和列

这是返回的json,带有错误的null值

返回的json具有错误的空值

这是模特

@Entity
@Table(name = "allriddles")
public class Riddle {

/*
 * ATTRIBUTES
 */
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;
private int difficulty;
private String prize;
private String riddlerName;
private int levels;
private String riddleDescription;

/*
 * CONSTRUCTORS
 */
public Riddle() {}
public Riddle( String title, int difficulty, String prize, String riddlerName, int levels,
        String description) {
    super();
    
    this.title = title;
    this.difficulty = difficulty;
    this.prize = prize;
    this.riddlerName = riddlerName;
    this.levels = levels;
    this.riddleDescription = description;
}
}

这是我的呼叫服务的控制器

 @RestController
 public class GetRiddles {

@Autowired
RiddlesService rs; 


@RequestMapping("/Riddles")
public List<Riddle> getAllRiddles(){
    return rs.getAllRiddles();
}

所谓的服务

@Service
public class RiddlesService {

@Autowired
RiddlesRepository riddlesRepository;

public List<Riddle> getAllRiddles(){
    List<Riddle> riddles = new ArrayList<>();
    riddlesRepository.findAll()
    .forEach(riddles::add);
    

    return riddles;
}

原始界面

public interface RiddlesRepository extends CrudRepository<Riddle, Integer> {

}

最后是控制台输出,如果有帮助的话

跟踪

帐户:

我尝试了同样的事情,并得到了相似的结果。如您所说,它是在将spring.jpa.hibernate.ddl-auto修改为更新时发生的。最终添加了2个新列。

Hibernate: alter table allriddles add column riddle_description varchar(255)
Hibernate: alter table allriddles add column riddler_name varchar(255)

原始列名称为riddleDescription和riddlerName,其余保留为现有值。但是新列中将没有数据。因此,假设我重新创建了与您相同的问题,要么

  • 在数据库中:将数据从旧列(不带下划线)移动到新列(带下划线)并删除旧列。要么
  • 在数据库中:删除新列(带下划线)。在Riddle.java改变性质riddlerNameriddleDescriptionriddlernameriddledescription

我认为您可能在某些属性名称的所有小写字母和驼峰字母之间也有所更改。因为驼峰属性将作为下划线映射到数据库(例如,代码-riddleName-> db列-riddle_name),而所有小写字母都没有下划线(例如,代码-riddlername-> db列-riddlerName或riddlername)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在来自Angular前端的一些频繁的API请求之后,httpSession.getAttribute(“ userId”)在Spring Boot中返回null

Spring Boot:当返回的对象为null时,返回一个空的JSON而不是空的正文

如何在特定的URL上返回一些HTML文件-Spring Boot

为什么Spring Boot返回一个字符串而不是JSON

将一些JSON文件加载到Spring Boot应用程序中的最佳方法

如何在Spring Boot中禁用一些警告

Spring Boot Rest API枚举一些Java类型

Vaadin 在 Spring Boot 中缺少一些功能

Spring boot:排除一些自动配置的bean

Spring Boot-接口和一些功能实现

为什么当有一些非NULL值时MySQL GROUP_CONCAT返回NULL

为什么Spring的FactoryBean getObject返回null?

json 對象返回 null spring boot

为什么Spring MVC会报告“找不到类型为org.json.JSONObject的返回值的转换器”?

为什么 Spring Boot 父启动器会修改 surefire 默认值?

Spring Boot @WebMvcTest中模拟的方法返回值为null

Junit 5的Spring Boot单元测试为什么模拟返回null

为什么在使用Spring PropertySourcesPlaceholderConfigurer时Annotation @Value返回始终为null?

为什么Spring Boot休眠的OneToMany映射子级返回空值?

为什么在基于注释的Spring应用程序@Value中,默认值被解析为null?

保存后Spring Boot为关系实体返回空值

如何使用 spring boot 不返回 null 的对象值

Spring Boot 2-一些Graphite Metrics未从定制程序接收通用标签

Spring-boot 执行器:只有一些端点可以工作

在启动Spring Boot Scheduler之前如何进行一些预处理?

spring boot 1.5.2 没有加载一些抛出 Unsatisfied 依赖异常的类

Spring Boot无法正常工作并出现一些错误?

保护application.properties中的一些密钥-Spring Boot Application

我对'pom.xml' spring-boot 有一些问题