Tazz
Tazz

Reputation: 89

Can't discover endpoints url

So today i started using spring-boot, and my point is to create a rest api with spring-boot.

I created just a small project with a rest controller and a simple model. And i'm trying to call the method from the rest controller (with no success)

Controller:

@RestController

    public class RestController {

            @RequestMapping(method = RequestMethod.GET, 
                            produces = MediaType.APPLICATION_JSON_VALUE,
                            value = "/something")
            public @ResponseBody Something getSomething(){
                Something s = new Something ();
                return s;
            }
    }

Main:

@SpringBootApplication
public class SpringBootRestExampleApplication 

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRestExampleApplication.class, args);
    }
}

And an application properties:

server.port=8081
spring.data.rest.basePath=/micro

So if i have a basePath/port and a mapping.. normally i only need to use localhost:8081/micro/something . But for only reason when i use an API Client i got 404 not found for the endpoint /something. Anyone can point me if something is missing? (miss some declaration/..)?

I read other post from stack i tried to use (for example) RequestMappingHandlerMapping

Note: project running with tomcat 8. i already tried localhost:8081/something ; localhost:8081/micro, etc ; added some urlmapping annotation on the rest controller

Upvotes: 2

Views: 1317

Answers (3)

Ahmed Itani
Ahmed Itani

Reputation: 63

spring.data.rest.base-path will not work because it is specifically made for spring data-rest. You can check this ticket

https://github.com/spring-projects/spring-boot/issues/7816

As a workaround you can use what @oleg.cherednik has suggested or upgrade to spring boot 2.0 and use this

server.servlet.context-path=/micro

Upvotes: 1

J-Alex
J-Alex

Reputation: 7117

According to the documentation:

spring.data.rest.base-path= # Base path to be used by Spring Data REST to expose repository resources.

Try to use base-path instead of basePath.

spring.data.rest.base-path=/micro

Or try to set dispatcherServlet path like:

server.contextPath=/micro

For SpringBoot 2.0+:

server.servlet.context-path=/micro

Search logs for:

o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [$URL]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/root/test],methods=[GET]}" onto public java.lang.String com.test.Rest.getSomething()

Upvotes: 2

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

I would reccomend not to use server.contextPath or spring.data.rest.base-path. I faced with different beheviour of this.

Do declare a class with all paths of you API:

public static final class RequestPath {
    public static final String BASE = "/micro";

    public static final String SOMETHING = BASE + "/something";
}

And use it in your controllers:

@RestController
public class RestController {
    @RequestMapping(value = RequestPath.SOMETHING)
    @ResponseBody
    public Something getSomething() {}
}

Finally what you get:

  1. All paths in one file. This could help see it at the same time
  2. Documentation (in case you do not use Swagger or Raml)
  3. You do not care about base path settings, that could works differently (+ you definetly know correct request path to required endpoint).

Upvotes: 0

Related Questions