I have the following code in one of my controllers:
@Controller
@RequestMapping("/preference")
public class PreferenceController {
@RequestMapping(method = RequestMethod.GET, produces = "text/html")
public String preference() {
return "preference";
}
}
I am simply trying to test it using Spring MVC test as follows:
@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PreferenceControllerTest {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = webAppContextSetup(ctx).build();
}
@Test
public void circularViewPathIssue() throws Exception {
mockMvc.perform(get("/preference"))
.andDo(print());
}
}
I am getting the following exception:
Circular view path [preference]: would dispatch back to the current handler URL [/preference] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
What I find strange is that it works fine when I load the "full" context configuration that includes the template and view resolvers as shown below:
<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="webTemplateResolver">
<property name="prefix" value="WEB-INF/web-templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="2" />
<property name="cacheable" value="false" />
</bean>
I am well aware that the prefix added by the template resolver ensures that there are not "circular view path" when the app uses this template resolver.
But then how I am supposed to test my app using Spring MVC test? Has anyone got any clue?
For Thymeleaf:
I just began using spring 4 and thymeleaf, when I encountered this error it was resolved by adding:
This is happening because Spring is removing "preference" and appending the "preference" again making the same path as the request Uri.
Happening like this : request Uri: "/preference"
remove "preference": "/"
append path: "/"+"preference"
end string: "/preference"
This is getting into a loop which the Spring notifies you by throwing exception.
Its best in your interest to give a different view name like "preferenceView" or anything you like.
@Controller
→@RestController
I had the same issue and I noticed that my controller was also annotated with
@Controller
. Replacing it with@RestController
solved the issue. Here is the explanation from Spring Web MVC:try adding compile("org.springframework.boot:spring-boot-starter-thymeleaf") dependency to your gradle file.Thymeleaf helps mapping views.
This is how I solved this problem:
I use the annotation to configure spring web app, the problem solved by adding a
InternalResourceViewResolver
bean to the configuration. Hope it would be helpful.