首先我求你原谅我的英语,如果我在标题或内容方面做错了,这是我第一次。我尝试第一次进行api休息,我尝试获取一些已经将mysql保存为json的数据。休息API。但是ı在控制台和邮递员中得到java.lang.NullPointerException:“错误”:“内部服务器错误”。我该如何解决?还是我做错了什么?
我已经尝试了很多事情,所以您可以看到更多东西。但是ı无法解决ı无法获取数据的问题。
我的对象
@Entity
@Data // It's generate setters and getters.
@Table(name="universities")//It says name of the table in database.
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters =
true)
@EntityListeners(AuditingEntityListener.class)
public class UniversityDetailItem {
@Id
@Column(name = "api_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int api_id;
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "city")
private String city;
@Column(name = "founded_at")
private int founded_at; //I declare as a integer because the variable consists of 4 integers.
@Column(name = "web_page")
private String web_page;
@Column(name = "type")
private String type;
}
我的DAO类
public class UniversityDAO {
@Autowired
UniversityRepository universityRepository;
@Query(value = "SELECT * FROM sampledatabase.universities", nativeQuery = true)
public Iterable<UniversityDetailItem> findAll(){
return universityRepository.findAll();
}
}
我的控制器类
@RestController
public class UniversityController {
@Autowired(required = false)
UniversityDAO universityDAO;
@GetMapping("/get")
public @ResponseBody Iterable<UniversityDetailItem> getAllUniversities(){
return universityDAO.findAll();
}
@GetMapping("/universities") // try
public String ekmek(){
return "Merhaba ekmek canavarı hamiidin";
}}
入门应用程序
@SpringBootApplication
@EnableJpaAuditing
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
重新发布
@Repository
public interface UniversityRepository extends
JpaRepository<UniversityDetailItem,Long> {
}
应用程序属性
## Spring DATASOURCE (DatSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url= jdbc:mysql://localhost:3306/sampledatabase?
useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = ********
# Show or not log for each sql query
spring.jpa.show-sql = true
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen
database
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5Dialect
# Naming strategy
#
spring.jpa.hibernate.naming-strategy =
org.hibernate.cfg.ImprovedNamingStrategy
#Hibernate ddl auto (create,create-drop,validate,update)
spring.jpa.hibernate.ddl-auto=update
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
mySQL表已创建
Columns:
id int(11) PK
id_api int(11)
name varchar(63)
city varchar(15)
founded_at int(11)
web_page varchar(32)
type varchar(6)
created_at datetime
updated_at datetime
api_id int(11)