0

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'
Alfredo Marin
  • 195
  • 1
  • 1
  • 14

1 Answers1

1

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.s.b.w.embedded.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 .

R.G
  • 6,436
  • 3
  • 19
  • 28
  • Tried this but got the same issue. I also removed the "@GetMapping" and the "value = ''" because sounds redundant but still getting 404. And when i make a call like https://localhost:8787/authorization/api/cities he hits my back-end but says that he can't parse a HTTP charachter on the requested path or something. – Alfredo Marin Jan 18 '20 at 11:24
  • What is the context path logged when the application starts up ? Please update the question with the error and the latest code you tried with and spring boot version – R.G Jan 18 '20 at 11:32
  • 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' This is the path that logged to me! – Alfredo Marin Jan 18 '20 at 12:07
  • what happens when you use a browser to hit this url ? Also , can you try modifying the method as follows @Override @RequestMapping(method = RequestMethod.GET) public String loadAll(){ return "test"; } – R.G Jan 18 '20 at 12:20
  • I've tried exactly as you recommended and the same happens. Browser response: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jan 18 09:57:36 BRT 2020 There was an unexpected error (type=Not Found, status=404). No message available – Alfredo Marin Jan 18 '20 at 12:59
  • Make sure the '@RestController' falls in the radar of '@ComponentScan' – R.G Jan 18 '20 at 13:07
  • How can i do this? There's a way to see which controllers are on the component scan? – Alfredo Marin Jan 18 '20 at 15:07
  • Make sure @SpringBootApplication annotation is at the top package level . Else @ComponentScan(basePackages ={“package”}) on the class you run. Basically making sure your Controller gets autodetected when the app starts up – R.G Jan 18 '20 at 15:38
  • Finnally that works!! I was using my folders like this: com.xxx.main.AuthenticationMain.class. Now i'm using as com.xxx.AuthenticationMain.class and that works out! Now i have some problems with the GenericRepository but at least the endpoints are responding correctly! Thanks @R.G! – Alfredo Marin Jan 18 '20 at 17:50
  • Great !! Glad that I could help . If it was componentscan that created issues , shall I update the answer and you can mark it closed ? – R.G Jan 18 '20 at 17:52
  • Yes please!! The problem really was the @ComponentScan! Hope this question help someone else like me! Thanks again R.G! – Alfredo Marin Jan 18 '20 at 18:56