'SpringBoot RestAPI Controller methods are not working
I wrote a few service methods. When I implement CommandLineRunner to my SpringApplication and try to execute service methods in run methods I can see they're working.
I mean like this approach, I can reach the data via service methods. So I can say yes my services are working right.
public void run(String... args) throws Exception {
postService.getAllPost()
.stream()
.forEach(entry -> System.out.println(entry.getTitle()));
}
But When I try to use my controllers via postman. For the User class, everything is fine.
public List<UserDto> getAllUsers() {
return userRepository.findAll()
.stream()
.map(userMapper::toDto)
.collect(toList());
}
@GetMapping("/get")
public ResponseEntity<List<UserDto>> getAll() {
return ResponseEntity.ok(userService.getAllUsers());
}
But Comment and Post are not working. The methods are almost same. I was wondering is it about my entities relation? Because in User entity I just have 3 different field which is Long, String , String . But the other entities are related with each others. With OneToMany relational. And I dont trust about my business plan. I mean post class includes user and comment object. Comment class includes user object etc.
This is controller method.
@GetMapping("/getAll")
public ResponseEntity<List<CommentDto>> fetchAll() {
return ResponseEntity.ok(commentService.getAllComments());
}
This is service method.
public List<CommentDto> getAllComments() {
return commentRepository.findAll().stream()
.map(commentMapper::toDto)
.collect(Collectors.toList());
}
I have three different entity class.
Which is
@Entity
@Table(name = "user_table")
public class User{
@Id
private Long id;
private String userName;
private String password;
}
@Entity
@Table(name = "post_table")
public class Post {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id" , nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private User user;
private String title;
@Lob
@Column(columnDefinition = "text")
private String text;
}
@Entity
@Table(name = "comment_table")
public class Comment {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id" , nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Post post;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id" , nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private User user;
@Lob
@Column(columnDefinition = "text")
private String text;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
