0

Note: The code can be found here: https://github.com/silentsnooc/ema-server


So, I am cleaning up a quite messy project in which I try to serve static content as well as a small REST API. However, the application gets started as any other:

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

The REST API works for me but the web client is not. I've moved the web client resources into src/main/resources/static/

enter image description here

However, if I visit http://localhost:8080 what I get is 405 (Request method 'GET' not supported).

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:207) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:374) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1145) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
...

I do not understand what's missing - maybe I got the folder structure wrong?

Please let me know if you need any further information from my side.

1 Answer 1

2

As stated here @RestController is itself annotated with @ResponseBody which makes the response of your RequestMapping methods to be their return value. In your case that is the index.html string you're seeing in the web browser.

Replacing the @RestController annotation with @Controller should do the trick.


As for your error you have another class WordEmbeddingRestController which defines mappings for "/" as well, as "/" (or "" to be exact) is the default path value. Removing the @RestController annotation from this class will enable you to GET the desired html file.


Ignore that, GET is the default method if non was specified

For your edit:

You have to add the method you want to support to the @RequestMapping annotation for each of your methods. See this for more information. As an example:

@RequestMapping(path = "/", method = RequestMethod.GET)
public String index() {
    return "index.html";
}

Instead of using @RequestMapping with an explicit method you can also use shorthand annotations like @GetMapping and @PostMapping. Using these would reduce your code to someting like this:

@GetMapping("/")
public String index() {
    return "index.html";
}

Sign up to request clarification or add additional context in comments.

7 Comments

That definitely sounds correct. However, visiting localhost:8080 will still give me back a 405 error .. The log message (on the server) would be Request method 'GET' not supported
Do I actually need this index() endpoint? Shouldn't the application serve that by default? It's said that it looks automatically in static/, public/, etc in order to look for such content.
In any case: I just tried GetMapping("/") - unfortunately with the same result. :/
Btw. I have pushed the code to github.com/silentsnooc/ema-server
You do not actually need the mapping if you just want to serve static files. You only need it if you (as an example) want to fill in a template file instead of always serving the same static file. As for your error you have another class WordEmbeddingRestController which defines mappings for "/" as well, as "/" (or "" to be exact) is the default path value. Removing the @RestController annotation from this class will enable you to GET the desired html file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.