Spring Boot为什么会将某些值返回为null的JSON?

huangapple go评论57阅读模式
英文:

why is spring boot returning me a json with null for some values?

问题

I'm making a game for practice.

我正在做一个练习用的游戏。

I have a spring boot/Maven project connected to a MySQL database.

我有一个与MySQL数据库连接的Spring Boot/Maven项目。

I was able to setup an api that simply retrieves everything from my "allriddles" table.

我成功设置了一个API,它简单地从我的"allriddles"表中检索所有内容。

It worked before, but now the api returns a json where some values are null for some keys.

它以前运行正常,但现在API返回一个JSON,其中一些键的值为null。

The only thing i was playing with was the application.properties on the spring boot file. I was hopping between "update" and "none" for the spring.jpa.hibernate.ddl-auto.

我唯一尝试的事情是在Spring Boot文件的application.properties中进行更改。我在spring.jpa.hibernate.ddl-auto中尝试了"update"和"none"之间的切换。

What could I have done wrong?

我可能做错了什么?

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

这是application.properties文件的内容。

this is the database table and expected columns

这是数据库表和预期列。

this is the returned json with erroneous null values

这是带有错误空值的返回JSON。

here is the model

这是模型。

@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;
}

}

这是模型的定义。

this is my controller that calls a service

这是调用服务的控制器。

@RestController
public class GetRiddles {

@Autowired
RiddlesService rs; 

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

}

这是调用服务的控制器。

the service that calls crud

调用CRUD操作的服务。

@Service
public class RiddlesService {

@Autowired
RiddlesRepository riddlesRepository;

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

}

这是调用CRUD操作的服务。

the crud interface

CRUD接口。

public interface RiddlesRepository extends CrudRepository<Riddle, Integer> {

}

and finally the console output if thats of any help

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

I hope this helps! If you have any further questions, please feel free to ask.

英文:

I'm making a game for practice.

I have a spring boot/Maven project connected to a MySQL database. I was able to setup an api that simply retrieves everything from my "allriddles" table. It worked before, but now the api returns a json where some values are null for some keys. The only thing i was playing with was the application.properties on the spring boot file. I was hopping between "update" and "none" for the spring.jpa.hibernate.ddl-auto.
What could I have done wrong?

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

this is the database table and expected columns

Spring Boot为什么会将某些值返回为null的JSON?

this is the returned json with erronous null values

Spring Boot为什么会将某些值返回为null的JSON?

here is the model

@Entity
@Table(name = &quot;allriddles&quot;)
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;
}
}

this is my controller that calls a service

 @RestController
 public class GetRiddles {

@Autowired
RiddlesService rs; 


@RequestMapping(&quot;/Riddles&quot;)
public List&lt;Riddle&gt; getAllRiddles(){
	return rs.getAllRiddles();
}

the service that calls crud

@Service
public class RiddlesService {

@Autowired
RiddlesRepository riddlesRepository;

public List&lt;Riddle&gt; getAllRiddles(){
	List&lt;Riddle&gt; riddles = new ArrayList&lt;&gt;();
	riddlesRepository.findAll()
	.forEach(riddles::add);
	

	return riddles;
}

the crud interface

public interface RiddlesRepository extends CrudRepository&lt;Riddle, Integer&gt; {

}

and finally the console output if thats of any help

Spring Boot为什么会将某些值返回为null的JSON?

答案1

得分: 1

我尝试了同样的操作,得到了类似的结果。正如你所说,在将 spring.jpa.hibernate.ddl-auto 修改为 update 后发生了这种情况,它最终添加了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 中将属性 riddlerNameriddleDescription 更改为 riddlernameriddledescription

我猜想你可能在某个时候也更改了属性名称的大小写形式,因为驼峰形式的属性将映射到数据库中的下划线形式(例如,代码 - riddleName -> 数据库列 - riddle_name),而全小写形式将不包含下划线(例如,代码 - riddlername -> 数据库列 - riddlerName 或 riddlername)。

英文:

I tried the same thing, and got a similar result. It occurred, as you said, upon modifying spring.jpa.hibernate.ddl-auto to update. It ended up adding 2 new columns.

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

The original column names were riddleDescription and riddlerName and those remained with existing values. But the new columns will not have data in them. So assuming I recreated the same issue you had, either you have to

  • in the database: move data from old columns (without the underscore) to new columns (with the underscore) and remove old columns. Or
  • in the database: remove new columns (with the underscore). in Riddle.java change properties riddlerName and riddleDescription to riddlername and riddledescription.

I assume possibly you might have also changed at some point between all lower case and camelcase for those property names. Because camelcase properties will map to database as underscores (e.g. code - riddleName -> db column - riddle_name), while all lowercase will not have underscores (e.g. code - riddlername -> db column - riddlerName or riddlername).

答案2

得分: 1

这是Hibernate根据实体中定义的字段在表中命名列的默认方式。

要覆盖默认行为,您可以使用

@Column(name = "riddleName")
private String riddlerName;

@Column(name = "riddleDescription")
private String riddleDescription;
英文:

This is the default way in which hibernate names columns in the table based on the fields defined in the entity.

To override the default behaviour you can use

@Column(name = &quot;riddleName&quot;)
private String riddlerName;

@Column(name = &quot;riddleDescription&quot;)
private String riddleDescription;

huangapple
  • 本文由 发表于 2020年7月22日 13:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定