简体   繁体   中英

Spring boot endpoint path

I have some issues with my IntelliJ project. I'm trying to hit a endpoint with Postman but I don't even know what is the path to do so! I tried every path possible and nothing works! Just got 404 status. Here's my restController:

@RestController
@RequestMapping("/cities")
public class CityService extends GenericService<City> {

    @Autowired
    private CityRepository cityRepository;

    /**
     * Method that returns all the cities.
     * @return {@link List<City>}
     */
    @Override
    @RequestMapping(method = RequestMethod.GET)
    public List<City> loadAll(){
        System.out.println(new Date() + " Called LOAD-ALL.");
        return cityRepository.findAll();
    }
}

My genericService:

public class GenericService<T> {

    @Autowired
    GenericRepository<T> genericRepository;

    public GenericRepository<T> getGenericRepository(){
        return genericRepository;
    }

    /**
     * Method that returns all the generic entities.
     * @return {@link List<T>}
     */
    @GetMapping
    @RequestMapping(method = RequestMethod.GET)
    public List<T> loadAll(){
        System.out.println(new Date() + " Called LOAD-ALL.");
        return getGenericRepository().findAll();
    }

My config.properties:

spring.application.name = authorization-service
server.servlet.context-path = /authorization/api
#server.context-path = /authorization/api
#server.contextPath = /authorization/api
#server.module-path=/

spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.ddl = true
spring.jpa.show-sql = true

hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

server.port = 8787

eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}

eureka.instance.instance.instance-id = ${spring.application.name}:${server.port}:${random.int}

spring.cloud.loadbalancer.ribbon.enabled = false

spring.datasource.url = jdbc:mysql://localhost:3306/my_database? 
createDatabaseIfNotExist=true
spring.datasource.username = someUser
spring.datasource.password = somePass

I was trying to use this link on Postman http://localhost:8787/authorization/api/cities but nothing works. Someone can help me please? I'm kinda new on this thing of mapping endpoints. I'm using Spring Boot 2.2.2.RELEASE. The startup message is

2020-01-18 09:06:10.023  INFO 2808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8787 (http) with context path '/authorization/api'

Please try with the property.

server.servlet.context-path = /authorization/api

Also when the spring boot application starts up , the context path will be displayed in the logs as something below

[2m2020-01-18 08:41:04.513[0;39m [32m INFO[0;39m [35m13986[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.sbwembedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path '/authorization/api'

Also the following

@GetMapping
@RequestMapping(method = RequestMethod.GET, value = "")

means the same. Please check @GetMapping

Update Op had issues with Component scan and Controller classes were not auto detected .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM