0

I have created a spring boot application by using a Dynamic Web Project in eclipse with the following file setup: enter image description here

My spring boot configuration was not configured using maven and thus without a pom.xml file(I instead imported all dependencies needed). My App file contains the running of spring boot:

package Main;


import java.util.Collections;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App 
{

public static void main(String[] args) 
{
    SpringApplication app = new SpringApplication(App.class);
    app.setDefaultProperties(Collections
            .singletonMap("server.port", "9004"));
    app.run(args);
}

}

I am also attempting to render a html page called test.html:

<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
    <p>Number: <span th:text="${number}"></span></p>
    <p>Name: <span th:text="${firstName}"></span></p>
</body>

Render it via springboot as so:

package Main;

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


@Controller
@RequestMapping("/")
public class MainController 
{
@GetMapping("testing")
@ResponseBody
public String testPage(Model model) 
{
    String name = "John";
    model.addAttribute("number", 42);
    model.addAttribute("firstName", name);
    return "test";



}
}

The issue I am having is that it is returning the string test, rather then test.html. I have attempted using @RestController instead(removing @ResponseBody) with no luck. I am pretty sure it has to do with the URL to test.html but I am not sure. Any help would be appreciated.

1 Answer 1

1

Remove @ResponseBody and Spring will attempt to look up the view to return.

The reason why removing @ResponseBody but adding @RestController failed is the fact that @RestController is a meta-annotation combining @ResponseBody and @Controller i.e. nothing changed by doing that.

You can see on the Controller and ResponseBody documentation that the response body effectively means that the return value will be transformed to body of the http response rather than looking up a classical view for the controller.

Here's a working example of your code,

https://github.com/DarrenForsythe/so-67693611

I've additionally added a test that can verify the behavior without running the entire application,

package com.darrenforsythe.controllerview.so67693611;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@WebMvcTest
class MainControllerTests {

    private final MockMvc mockMvc;

    @Autowired
    MainControllerTests(MockMvc mockMvc) {
        this.mockMvc = mockMvc;
    }

    @Test
    void testViewShouldBeReturned() throws Exception {
        mockMvc.perform(get("/testing"))
                .andExpect(status().isOk())
                .andExpect(view().name("test"))
                .andExpect(model().attribute("firstName", "John"))
                .andExpect(model().attribute("number", 42));

    }
}

If you clone the repository and run the build-and-run.sh it should create the Spring Boot Jar, run it, and open the URL http://localhost:9004/testing.

My guess that your IDE has no synced the resources, a clean build might resolve it.

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

4 Comments

If I remove "@ResponseBody" in this case I get a 404 error that it cant find /testing. This is solved using "@RestController" instead but it also causes same issue of returning the string "test" rather than test.html
Yes, it cannot find the test view in that case. As mentioned ResponseBody is used to transform the return value to the body of a response e.g. when you want to return a JSON Object. I would ensure that your resource folders are being packaged when built
Yes but this is my whole issue. I am trying to figure out why spring boot cannot find the test.html. I have even attempted other methods such as return new ModelAndView("test.html"); and this is unsuccessful as well.
@JmanxC I've updated my answer with a working example and repository.

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.