Power_tile
Power_tile

Reputation: 810

Not able to get response with @Controller, while @RestController works fine

I am trying to map the URL /function/hash in my project to a specific HTML page html/hashcode.html. This is a spring boot project without using thymeleaf.

This is my code:

// package ...;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FunctionController {
    @RequestMapping("/function/hash")
    public String hashPage(Model m) {
        return "html/hashcode.html";
    }
}

The above code returns a 404 when I try to access localhost:8080/function/hash.

I also tried

@Controller
@RequestMapping("/function")
public class FunctionController {
    @RequestMapping("/hash")
    public String hashPage(Model m) {
        return "html/hashcode.html";
    }
}

which also yields a 404 when I go to localhost:8080/function/hash.

Directly using @RequestMapping("/hash") to map the page to /hash works, in case you wonder if the return value of the function is incorrect.

I also find that using multiple layer url like @RequestMapping("/api/test") is working in @RestController classes, but somehow it doesn't work in this @Controller class above.

Upvotes: 1

Views: 638

Answers (5)

Return "/html/hashcode.html"(prefix /), and create <project-root>/src/main/resources/static/html/hashcode.html

@Controller
public class FunctionController {
    @RequestMapping("/function/hash")
    public String hashPage(final Model m) {
        return "/html/hashcode.html";
    }
}

When return "html/hashcode.html":

o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/function/html/hashcode.html", parameters={}

On the other hand, when return "/html/hashcode.html":

o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/html/hashcode.html", parameters={}

Upvotes: 1

Hantsy
Hantsy

Reputation: 9261

If the html files are static resources, consider the static content support in Spring Boot.

Configure a spring.resources.static-locations to specify the resource localtions.

spring.resources.static-locations=file:/opt/files/,classpath:/static-files

And set the mapping pattern if you do not want to map it the root path.

pring.mvc.static-path-pattern=/content/**

(Or spring.webflux.static-path-pattern for Spring webflux application)

Now you can view the resources via http://localhost:8080/content/some.html

Upvotes: 0

Romil Patel
Romil Patel

Reputation: 13737

@RestController is combination of @Controller + @ResponseBody. While using @Controller we have to add the @ResponseBody with our methods. You can find more details here

@Controller
public class MappingController {

    @RequestMapping("/endpoint1") //returns 404
    public String endPoint1() {
        return "Hello endpoint1";
    }

    @RequestMapping("/endpoint2") //works well because of @ResponseBody
    public @ResponseBody String endPoint2() {
        return "Hello endpoint2";
    }
}

Add @ResponseBody and these both should work fine

@Controller
public class FunctionController {
    @RequestMapping("/function/hash")
    public @ResponseBody String hashPage(Model m) {
        return "html/hashcode.html";
    }
}

@Controller
@RequestMapping("/function")
public class FunctionController {
    @RequestMapping("/hash")
    public @ResponseBody String hashPage(Model m) {
        return "html/hashcode.html";
    }
} 

Upvotes: 0

sarwadnya deshpande
sarwadnya deshpande

Reputation: 119

add @ResponseBody annotation, The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while the @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody

if u add @ResponseBody it will work. use below code

// package ...;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FunctionController {

    @RequestMapping("/function/hash")
    public @ResponseBody String hashPage(Model m) {
        return "html/hashcode.html";
    }
}

Upvotes: 0

Sivaraj Velayutham
Sivaraj Velayutham

Reputation: 187

use path in request mapping.

Ex:

@RequestMapping(path="/hash")

Upvotes: 0

Related Questions