Home > Enterprise >  Circular View Path Exception in Spring Boot
Circular View Path Exception in Spring Boot

Time:10-02

I'm trying to return "index.html" page for every request mapping with below controller:

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


@Controller
public class ReactWebController {
    @RequestMapping(value = {"/", "/login/**", "/logout/**", "/forgotpassword/**", "/home/**", "/dashboard/**"
            , "/user/**", "/profile/**", "/catalog/**", "/permission/**", "/diconnect/**", "/nsm/**", "/session/**"
            , "/settings/**", "/log/**", "/config/**", "/delete/**", "/initialize/**", "/initial/**", "/changepassword/**",
            "/portalconnect/**", "/portal/**", "/portal/user/**", "/portal/profile/**", "/portal/permission/**",
            "/portal/catalog/**", "/portal/metadata-browse/**","/portal/search/**","/portal-object-overview/**"})
    public String react() throws Exception {
        return "index.html";
    }
}

The thing is, when I refresh URL it is working when the path is like this: http://localhost/MartServer/user, but not when it has multiple folder kind of paths e.g. http://localhost/MartServer/portal/user. I get below exception details:

ERROR [/MartServer].[dispatcherServlet].log: Servlet.service() for servlet [dispatcherServlet] in context with path [/MartServer] threw exception [Circular view path [index.html]: would dispatch back to the current handler URL [/MartServer/portal/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [index.html]: would dispatch back to the current handler URL [/MartServer/portal/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

I tried to add addViewControllers() and addResourceHanlders() as well, but no luck. Can anyone help me how to fix this. Thanks!

Frontend is React JS, index.html is generated after "npm run build" and is used for controller

CodePudding user response:

Try implementing the below mentioned properties and see if they help your case:

  1. Use @RestController instead of @Controller on your ReactWebController class.

  2. Add a custom context-path to the application.properties file of your spring-boot application:

server.servlet.context-path=MartServer/portal

  1. You could also make use of a View Resolver. It can be configured in the application.properties file, as shown below:
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.html

Check out the official Spring Boot documentation for further details on configurable properties.

CodePudding user response:

As far as I see it is correct to throw you an exception because: you call the http://localhost/MartServer/portal/user which results the path http://localhost/MartServer/portal/index.html. You defined the paths "/portal/**" and "/portal/user/**" in your controller => the path match again.

I think that is better to review all your paths.

For example if you let "/portal/**" it will also match the path for http://localhost/MartServer/portal/user. But this depends of what you want to achieve.

  • Related