Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RestController("/xxx") mapping is ignored

Tags:

java

spring

I'am trying to create a simple, annotation based rest service which have two distinct endpoints with endpoints specified by value of @RestController annotations. The problem is that the url provided in those annotations are ignored.

@RestController("/books")
public class BooksApi {

@RequestMapping("/getBook")
public String getBook() {
    return "book";
}

@RestController("/movies")
public class MoviesApi {

@RequestMapping("/getMovie")
public String getMovie() {
    return "movie";
}

}

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "application.webservices.apis")
public class ApiConfiguration {
}

public class ApisServletDispacher extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] {ApiConfiguration.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[0];
}

@Override
protected String[] getServletMappings() {
    return new String[] {"/"};
}
}

So far the working url is: http://localhost:8080/getBook and I would like to have it changed to: http://localhost:8080/books/getBook

I'm running on wildfly 10, to war packaged application.

Why is xxx mapping in @RestController("XXX") ignored ?

like image 428
Leroy Avatar asked Oct 16 '25 07:10

Leroy


1 Answers

Try to use this:

@RestController
@RequestMapping("/books")
public class BooksApi {...
like image 173
Abdelhak Avatar answered Oct 18 '25 20:10

Abdelhak