I have a Spring Boot application and I have implemented a base controller that handles CRUD operations for all my entities. I have also created a BrandController that extends the base controller and a BrandRepository that implements CrudRepository. The problem is that when I try to access the endpoints for the BrandController such as /api/brands, I get a 404 error, but I can access them on /brands How can I fix this so that the endpoints are accessible with /api/entitys?
Here is the code for the BrandController:
package parc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import parc.model.concrete.Brand;
import parc.repository.BrandRepository;
@RestController
@RequestMapping("/api/brands")
public class BrandController extends BaseController<Brand, BrandRepository> {
private final BrandRepository repository;
public BrandController(BrandRepository repository) {
super(repository);
this.repository = repository;
}
}
Here is the code for the BaseController:
package parc.controller;
import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.*;
import java.util.List;
public class BaseController<T, R extends CrudRepository<T, Long>> {
private R repository;
public BaseController(R repository) {
this.repository = repository;
}
@GetMapping("/")
public List<T> getAll() {
return (List<T>) repository.findAll();
}
@PostMapping("/")
public T create(@RequestBody T entity) {
return repository.save(entity);
}
@GetMapping("/{id}")
public T getById(@PathVariable long id) {
return repository.findById(id).orElse(null);
}
@PutMapping("/{id}")
public T update(@PathVariable long id, @RequestBody T entity) {
return repository.save(entity);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
repository.deleteById(id);
}
}
And finally the code for the BrandRepository:
package parc.repository;
import org.springframework.data.repository.CrudRepository;
import parc.model.concrete.Brand;
public interface BrandRepository extends CrudRepository<Brand, Long> {
}
I'm not a pro in Spring Boot so I'll appreciate any kind of help!
What do you have in application.yml? maybe setting the following code will work:
in application.yml:
and the BrandController: