【发布时间】:2020-07-22 05:27:38
【问题描述】:
我正在制作一个练习游戏。
我有一个连接到 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
这是模型
@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();
}
调用 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 界面
public interface RiddlesRepository extends CrudRepository<Riddle, Integer> {
}
如果有任何帮助,最后是控制台输出
【问题讨论】:
-
你能添加从stacktrace生成的sql吗
-
请分享您返回此 JSON 的 api 代码
-
@Kandy 我添加了控制台输出
-
@Sunil 我使用了 RestController、Service 和 CRUD jpa。我将它们添加到代码中。我的道歉
-
也添加控制器部分
标签: java mysql spring-boot