I am trying to create a blog apis but Getting null values in postman while fetching data from database although data is present in database using springboot, when i am trying to get data by userId or categoryId. it's giving me null value as shown.
{ "postTitle": null, "postContent": null, "postImageName": null,
"postAddedDate": null, "category": null, "user": null }
// Controller Class
@RestController
@RequestMapping("/api/")
public class PostController {
@Autowired
private PostService postService;
// create
@PostMapping("/user/{userId}/category/{categoryId}/posts")
public ResponseEntity<PostDto> createPost(@RequestBody PostDto postDto, @PathVariable Integer userId,
@PathVariable Integer categoryId) {
PostDto createdPost = this.postService.createPost(postDto, userId, categoryId);
return new ResponseEntity<PostDto>(createdPost, HttpStatus.CREATED);
}
// get post by user
@GetMapping("/user/{userId}/posts")
public ResponseEntity<List<PostDto>> getPostByUser(@PathVariable Integer userId) {
List<PostDto> posts = this.postService.getPostByUser(userId);
return new ResponseEntity<List<PostDto>>(posts, HttpStatus.OK);
}
// get post by category
@GetMapping("/category/{categoryId}/posts")
public ResponseEntity<Set<PostDto>> getPostByCategory(@PathVariable Integer categoryId) {
Set<PostDto> posts = this.postService.getPostByCategory(categoryId);
return new ResponseEntity<Set<PostDto>>(posts, HttpStatus.OK);
}
}
//Post Entity Class
@Entity
@Table(name = "post")
@Getter
@Setter
@NoArgsConstructor
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer postId;
private String postTitle;
@Column(length = 10000)
private String postContent;
private String postImageName;
private Date postAddedDtae;
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
@ManyToOne
private User user;
}
//Post Dto Class
@Getter
@Setter
@NoArgsConstructor
public class PostDto {
private String postTitle;
private String postContent;
private String postImageName;
private Date postAddedDate;
private CategoryDto category;
private UserDto user;
}
//Post Repo class
public interface PostRepo extends JpaRepository<Post, Integer> {
List<Post> findByUser(User user);
Set<Post> findByCategory(Category category);
}
//Post Service Class
public interface PostService {
// create
PostDto createPost(PostDto postDto, Integer userId, Integer categoryId);
// update
Post updatePost(PostDto postDto, Integer posId);
// getAll
List<PostDto> getAllPost();
// getSinglepost
Post getPostByid(Integer PostId);
// Delete
void deletePost(Integer postId);
// getPOstByCategory
Set<PostDto> getPostByCategory(Integer categoryId);
// getAllPostByUser
List<PostDto> getPostByUser(Integer userId);
// Search Post
List<PostDto> searchPosts(String keyword);
}
//Post ServiceImplmentation class
@Service
public class PostServiceImpl implements PostService {
@Autowired
private PostRepo postRepo;
@Autowired
private ModelMapper modelMapper;
@Autowired
private UserRepo userRepo;
@Autowired
private CategoryRepo categoryRepo;
@Override
public PostDto createPost(PostDto postDto, Integer userId, Integer categoryId) {
User user = this.userRepo.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("User", "User Id", userId));
Category category = this.categoryRepo.findById(categoryId)
.orElseThrow(() -> new ResourceNotFoundException("Category", "Category Id", categoryId));
Post post = this.modelMapper.map(postDto, Post.class);
post.setPostImageName("Default.png");
post.setPostAddedDtae(new Date());
post.setUser(user);
post.setCategory(category);
Post newPost = this.postRepo.save(post);
return this.modelMapper.map(newPost, PostDto.class);
}
@Override
public Post updatePost(PostDto postDto, Integer posId) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<PostDto> getAllPost() {
// TODO Auto-generated method stub
return null;
}
@Override
public Post getPostByid(Integer PostId) {
// TODO Auto-generated method stub
return null;
}
@Override
public void deletePost(Integer postId) {
// TODO Auto-generated method stub
}
@Override
public Set<PostDto> getPostByCategory(Integer categoryId) {
Category cat = this.categoryRepo.findById(categoryId)
.orElseThrow(() -> new ResourceNotFoundException("Category", "Category Id", categoryId));
Set<Post> posts = this.postRepo.findByCategory(cat);
Set<PostDto> postDtos = posts.stream().map((post) -> this.modelMapper.map(posts, PostDto.class))
.collect(Collectors.toSet());
return postDtos;
}
@Override
public List<PostDto> getPostByUser(Integer userId) {
User user = this.userRepo.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("User", "User Id", userId));
List<Post> posts = this.postRepo.findByUser(user);
List<PostDto> postDtos = posts.stream().map((post) -> this.modelMapper.map(posts, PostDto.class))
.collect(Collectors.toList());
return postDtos;
}
@Override
public List<PostDto> searchPosts(String keyword) {
// TODO Auto-generated method stub
return null;
}
}
Please check and let me know where i am going wrong...