I've created a SpringBoot application that interacts with a react frontend UI. I wanted to ensure that data was getting passed in correctly through the API so I tried a GET Request in Postman. The response is returning an empty object instead of the data from the database, which I want. I can POST with no problem and compile with no errors. All files are in the same package. I have two rows in the table that correspond with the two open brackets, I'm just getting no data.
Entity Class
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "game_articles")
public class Articles {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "reviewer")
private String reviewer;
@Column(name = "review")
private String review;
@Column(name = "path")
private String path;
@Column(columnDefinition = "integer default 0")
private int comments;
public Articles(String title, String reviewer, String review, String path, int comments){
this.title = title;
this.reviewer = reviewer;
this.review = review;
this.path = path;
this.comments = comments;
}
}
Controller: Commented method was my first attempt.
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("/api/v1")
@RestController
@AllArgsConstructor
public class ArticlesController {
@Autowired
ArticlesRepository articlesRepository;
@GetMapping("/articles")
public List<Articles> getAllArticles(){
// createArticle();
return articlesRepository.findAll();
}
public void createArticle(){ //ignore
Articles a = new Articles(1L,"Title Test", "TyTest", "This is a test review", "path/totest", 10);
articlesRepository.save(a);
}
// @GetMapping("/articles")
// public ResponseEntity<List<Articles>> getAllArticles(){
// try {
//// articlesRepository.findAll().forEach(articles::add);
// return new ResponseEntity<>(articles, HttpStatus.OK);
// }
// catch (Exception e){
// return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
// }
// }
}
Repository
@Repository
public interface ArticlesRepository extends JpaRepository<Articles, Long> {
}
SQL Query in the logs. I'm not sure why the columns are being referenced as articles0 but it had no effect on adding data to the database.
Hibernate:
select
articles0_.id as id1_0_,
articles0_.comments as comments2_0_,
articles0_.path as path3_0_,
articles0_.review as review4_0_,
articles0_.reviewer as reviewer5_0_,
articles0_.title as title6_0_
from
game_articles articles0_
I tried to replicate your issue, as you are using lombok, I suggest you try using @Data
in your Articles
entity:
@Entity
@Data
@Table(name = "game_articles")
public class Articles {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "reviewer")
private String reviewer;
@Column(name = "review")
private String review;
@Column(name = "path")
private String path;
@Column(columnDefinition = "integer default 0")
private int comments;
}
It will generate all @Getter
and @Setter
methods for the entity, which you need for the entity when querying data from your repository, as well as @RequiredArgsConstructor
, @ToString
and @EqualsAndHashCode
Hope it can help you a bit.