0

The application which has the pom.file below uses Spring Boot & Spring MVC & Rest template. If I deploy the app (or start with Spring boot Application.java class) and navigate to http://localhost:8080/app-name/user/{userID} then I get this exception:

HTTP Status 404 - /poc-app/user/WEB-INF/views/user.jsp

As you can see, it is appending the view with "user". Here is the code, configuration and pom.xml file:

@Controller
public class UserController {

@RequestMapping(value = "/user", method = RequestMethod.GET)
public String listAllUsers(Model model) {
    model.addAttribute("users", getUsers());
    return "user-list";
}

@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public String showUserDetail(@PathVariable("userId") int userId, Model model) {
            //not checking the list with id, this is just for test
    UserBean bean1 = new UserBean();
    bean1.setBinNumber("123456");
    bean1.setFullName("User One");
    bean1.setGuid("abcdef");
    bean1.setShortName("User1");

    model.addAttribute("user",bean1);
    return "user";
}

private List<UserBean> getUsers(){
    List<UserBean> list = new ArrayList<UserBean>();

    UserBean bean1 = new UserBean();
    bean1.setBinNumber("123456");
    bean1.setFullName("User One");
    bean1.setGuid("abcdef");
    bean1.setShortName("User1");

    UserBean bean2 = new UserBean();
    bean2.setBinNumber("987654");
    bean2.setFullName("User Two");
    bean2.setGuid("xyzabc");
    bean2.setShortName("User2");

    UserBean bean3 = new UserBean();
    bean3.setBinNumber("555555");
    bean3.setFullName("User Three");
    bean3.setGuid("hghghg");
    bean3.setShortName("User3");

    list.add(bean1);
    list.add(bean2);
    list.add(bean3);

    return list;
}

}


application.properties file

server.port: 8080
management.port: 8001
management.address: 127.0.0.1

spring.view.prefix: WEB-INF/views/
spring.view.suffix: .jsp

Spring Boot Application.java

@ComponentScan("com.company.project")
@Configuration
@SpringBootApplication
public class ProjectAdminWebPocApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(VoucherAdminWebPocApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder   application) {
        return application.sources(VoucherAdminWebPocApplication.class);
    }

    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.project</groupId>
    <artifactId>poc-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>poc-app</name>
    <description>Proof of concept project for Spring Boot</description>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Note: there is no problem if I navigate to http://localhost:8080/app-name/user - this works perfectly fine

2
  • 1
    Your view prefix should be /WEB-INF/views/... Notice the leading /... Commented Apr 15, 2016 at 9:26
  • Hi @M.Deinum thanks a lot. It helped! And this again proved that simple things can cause big problems :) btw if you could add this as an answer I could happily mark as accepted answer. Commented Apr 15, 2016 at 9:41

1 Answer 1

2
spring.view.prefix: WEB-INF/views/

You have your prefix configured as relative URLs instead of absolute URLs. Hence it will be added to the incoming request. To fix this make the URL absolute by adding a / to the beginning of it.

spring.view.prefix: /WEB-INF/views/
Sign up to request clarification or add additional context in comments.

Comments

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.