Reputation: 621
I am trying to create an api for a user to have the ability to add products to a specific category such that when, getCategory by id is called it gives a result as follows:
{
"product": [
{
"productId": 1,
"productName": "TestProdut1"
},{
"productId": 2,
"productName": "TestProdut2"
}
],
"categoryId": 1,
"categoryName": "Test1",
"parentCategoryId": 123
}
Please note i am using a postgres database
This is what i have done so far but when i try to get a category there is no response back. When using swagger the response comes back as no content
Category Entity :
@Entity
@Table(name = "category")
public class Category {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "category_id", unique = true, nullable = false)
private Long id;
@Column(nullable = false)
private String categoryName;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "categories", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Product> products = new ArrayList<Product>();
@ManyToOne
@JoinColumn(name = "parent_id")
@JsonIgnore
private Category parentId;
@OneToMany(mappedBy = "parentId")
private List<Category> subCategories = new ArrayList<>();
}
Product Entity :
@Entity
@Table(name = "products")
public class Product {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private Long id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category_id")
private Category categories;
}
Category Repository :
public interface CategoryRepository extends CrudRepository<Category, Long>{
public List<Category> findByCategoryName(String categoryName);
}
Category Service :
@Service
public class CategoryService {
@Autowired
private CategoryRepository categoryRepository;
public void addCategory(Category category) {
categoryRepository.save(category);
}
public List<Category> getAllCategories() {
List<Category> categoryList = new ArrayList<>();
categoryRepository.findAll().forEach(categoryList::add);
return categoryList;
}
public List<Category> getAllCategoriesByName(String categoryName) {
List<Category> categoryList = new ArrayList<>();
categoryList = categoryRepository.findByCategoryName(categoryName);
return categoryList;
}
public Optional<Category> getCategoryById(Long categoryId) {
Optional<Category> category = categoryRepository.findById(categoryId);
return category;
}
public void deleteCategory(Long id) {
categoryRepository.deleteById(id);
}
}
Category Controller :
@RestController
@Api(value = "CategoryAPI", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping("/ss")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping(method=RequestMethod.POST , value="/category/add", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> addCategory(@RequestBody Category category) {
categoryService.addCategory(category);
return new ResponseEntity<Boolean>(true, HttpStatus.CREATED);
}
@RequestMapping(method= RequestMethod.GET, value="/category/get/by/id/{categoryId}")
public void getCategoryById(@PathVariable Long categoryId) {
categoryService.getCategoryById(categoryId);
}
@RequestMapping(method= RequestMethod.GET, value="/category/get/by/name/{categoryName}")
public void getCategoryByName(@PathVariable String categoryName) {
categoryService.getAllCategoriesByName(categoryName);
}
@RequestMapping(method= RequestMethod.GET, value="/all/category")
public void getCategories() {
categoryService.getAllCategories();
}
}
Upvotes: 0
Views: 345
Reputation: 1213
It is because you are having void return type when retrieving the list of categories and that is why you are not getting any response.
@RequestMapping(method= RequestMethod.GET, value="/category/get/by/name/{categoryName}")
public ResponseEntity<List<Category>> getCategoryByName(@PathVariable String categoryName) {
return new ResponseEntity<>(categoryService.getAllCategoriesByName(categoryName),HttpStatus.OK);
}
Upvotes: 2