3

I am unable to render html page in springboot. Here is code...

@RestController
    public class ProductController {
        @Autowired
        ProductService service;
    
        @InitBinder
        public void initBinder(WebDataBinder webDataBinder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            dateFormat.setLenient(false);
            webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        }
        
        @RequestMapping(value = { "/", "/home" })
        public ModelAndView home() {
            System.out.println("sdasasas");
            return new ModelAndView("home");
        }
    
    

but whenever i hit http://localhost:8080/home it shows following logs

-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9,*/*;q=0.8]
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : View name 'home', model {}
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/WEB-INF/html/home.html]
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/WEB-INF/html/home.html", parameters={}
2019-07-31 16:05:25.356 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/","/"]
2019-07-31 16:05:25.356  WARN 14850 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Path with "WEB-INF" or "META-INF": [WEB-INF/html/home.html]
2019-07-31 16:05:25.356 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
dazito
  • 7,740
  • 15
  • 75
  • 117
Sahil
  • 59
  • 1
  • 10

4 Answers4

2

You should always use @Controller Annotation while using Spring MVC. @RestController is different from @Controller.

@Controller returns a view, used in MVC applications,

While @RestController returns a response(Mostly Json), it converts your your java object to Json by using Jackson jar, and these are used for api's.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
2

Avoid @RestController for MVC Based Application which has to return a view. It is mainly used for REST APIs. While @Controller can return a view

More On @RestController:

enter image description here

  • This annotation is a specialized version of @Controller which adds @Controller and @ResponseBody annotation automatically. so we do not have to add @ResponseBody to our mapping methods. That means @ResponseBody is default active.
  • If you use @RestController you cannot return a view (By using Viewresolver in Spring/Spring-Boot)
  • @RestController also converts the response to JSON/XML automatically as @ResponseBody makes the returned objects to something that could be in the body, e.g. JSON or XML

Controller vs RestController


@RestController
    public class ProductController {
        @Autowired
        ProductService service;

        @RequestMapping(value = { "/", "/home" })
        public @ResponseBody ModelAndView home() {
            System.out.println("sdasasas");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("home");
            return modelAndView;
        }
}

Project Structure enter image description here

No need to specify spring.mvc.view.prefix=/WEB-INF/html/ spring.mvc.view.suffix=.html

Also, make sure you don't have any additional class with @EnableWebMvc annotation. This can mess up the spring-boot autoconfiguration.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • I have tried @Controller but that also doesn't worked. – Sahil Jul 31 '19 at 13:15
  • Hello @Sahil, Can you update the question with new error – Romil Patel Jul 31 '19 at 13:18
  • @Sahil Also add `@ResponseBody` as I have added in answer and let me know how its work – Romil Patel Jul 31 '19 at 13:19
  • here i debug log after using above options..... 2019-07-31 18:57:38.785 WARN 32127 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/html/home.html] 2019-07-31 18:57:38.786 DEBUG 32127 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found 2019-07-31 18:57:38.786 DEBUG 32127 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404 – Sahil Jul 31 '19 at 13:28
  • @Sahil After adding ResponseBody are you getting this? and where you have kept home.html file – Romil Patel Jul 31 '19 at 13:30
  • Yes @Patel. I have placed home.html under webapp/WEB-INF/html. and in application.properties here is configured prefix path----spring.mvc.view.prefix=/WEB-INF/html/ spring.mvc.view.suffix=.html – Sahil Jul 31 '19 at 13:33
  • Hello @Sahil can you add your project structure – Romil Patel Jul 31 '19 at 13:40
  • @Sahil Have you manually created WEB-INF as there is no default WEB-INF is ther e for Spring Boot – Romil Patel Jul 31 '19 at 13:42
  • I have checked using above project structure also but that also doen't worked. Can you please share your email or something so that i can share project with you. I am still getting resource not found. – Sahil Jul 31 '19 at 14:24
  • Hi @patel i have shared the project with you. Please check and thnx for your assistance. – Sahil Jul 31 '19 at 14:29
  • @Sahil 403, please provide the access – Romil Patel Jul 31 '19 at 14:31
1

@RestController is not meant to be used to return views to be resolved. It is supposed to return data which will be written to the body of the response

        @RestController     //change it to @Controller
        public class ProductController {
        @Autowired
        ProductService service;

This difference between @Controller and the @RestController annotation is that @Controller is to create a Map of model object and find a view but the @RestController simply returns the object and object data is directly written into HTTP response as JSON or XML.

Rajendra Gupta
  • 381
  • 1
  • 16
1

If you are returning a view page like .jsp from the controller, and it's throwing an error saying cannot locate resource .jsp or 404 not found, then try adding these dependencies to your pom file, it worked for me.

 <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
ANUP SAJJAN
  • 1,458
  • 13
  • 17