API restful @mantytoone JPA, Spring boot - 404 not found

0

The api rest of these two entities I'm testing in a postman that gives me 404, eclipse compiles well the project, so I guess it is something wrong raised in the logic of my service or controller, I do not know which of the I am bad 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

    
asked by Diego 18.11.2018 в 10:55
source

1 answer

1

The problem is that server.servlet.context-path = /api only works if the packing is war and not jar "configuration of pom.xml". If you want the root path to be "/ api" you have to add an @RequestMapping at the class level:

@RestController
@RequestMapping("/api")
public class InstitutoController {

  @RequestMapping(value = "/institutos", method = RequestMethod.GET)
  public List<InstitutoEntity> getInstitutos() {
        ...
    }

}

when initializing spring in console you will see with log of mapped routes, it can be very useful when there are problems of routes:

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api],methods=[GET]}" onto public ......
    
answered by 23.01.2019 в 23:47