I am testing the rest api of these two entities in a postman that gives me 404, eclipse compiles the project well, so I guess it is something wrong in the logic of my service or controller, I do not know which of the I am wrong or if in both classes.
Below I leave the tables an institution has many institutes
@Entity(name="Instituto")
@Table(name="instituto")
public class InstitutoEntity {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column (name = "ID", nullable = false, updatable = false)
private int id;
@ManyToOne
@JoinColumn(name = "ID_INSTITUCION", nullable = false)
private InstitucionEntity institucion;
@Column (name = "NOMBRE", length = 30, nullable = false)
private String nombre;
@Column (name = "COLOR_HEXA", length = 30, nullable = false)
private String color_hexa;
public InstitutoEntity() {
super();
// TODO Auto-generated constructor stub
}
public InstitutoEntity(int id, InstitucionEntity institucion, String nombre, String color_hexa) {
this.id = id;
this.institucion = institucion;
this.nombre = nombre;
this.color_hexa = color_hexa;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public InstitucionEntity getInstitucion() {
return institucion;
}
public void setInstitucion(InstitucionEntity institucion) {
this.institucion = institucion;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getColor_hexa() {
return color_hexa;
}
public void setColor_hexa(String color_hexa) {
this.color_hexa = color_hexa;
}
@Override
public String toString() {
return "InstitutoEntity {"+
"id=" + id + ","
+ " institucion=" + institucion + ", "
+ "nombre=" + nombre + ", "
+ "color_hexa="+ color_hexa + "}";
}
}
>
@Entity (name = "Institucion")
@Table (name = "institucion_educativa")
public class InstitucionEntity {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column (name = "ID", nullable = false, updatable = false)
private int id;
@Column (name = "NOMBRE", length = 30, nullable = false)
private String nombre;
public InstitucionEntity () {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
public interface InstitutoRepository extends JpaRepository<InstitutoEntity, Integer>{
InstitutoEntity getInstitutoById(int id);
List<InstitutoEntity> getAllInstituto();
}
`
@Service
public class InstitutoService {
@Autowired
InstitutoRepository institutoRepository;
public List<InstitutoEntity> getAllInstituto() {
return institutoRepository.findAll();
}
public Optional<InstitutoEntity> getInstitutoById(int id) {
return institutoRepository.findById(id);
}
@RestController
public class InstitutoController {
@Autowired
private InstitutoService institutoService;
@RequestMapping(value = "/institutos", method = RequestMethod.GET)
public List<InstitutoEntity> getInstitutos() {
return institutoService.getAllInstituto();
}
@RequestMapping(value = "/instituto/{id}", method = RequestMethod.GET)
public Optional<InstitutoEntity> getInstituto(@PathVariable("id")int id) {
return institutoService.getInstitutoById(id);
}
}
properties
spring.main.banner-mode=off
server.servlet.context-path = /api
server.port = 8080
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
# Logger configurations.
logging.level.com.capgemini.serviciosya = DEBUG
logging.level.org.springframework.data = DEBUG
logging.level.org.hibernate.type = TRACE
# Hibernate
hibernate.dialect = org.hibernate.dialect.MariaDB53Dialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = validate
entitymanager.packagesToScan = ar.edu.unaj.reports.entity
spring.profiles.active=dev
The problem is that it
server.servlet.context-path = /api
only works if the packing is war and not jar "configuration pom.xml". If you want the root path to be "/api" you have to add a @RequestMapping at the class level:When initializing spring in the console you will see a log of the mapped routes, it can be very useful when there are route problems: