user7085085
user7085085

Reputation:

Spring Maven + IntelliJ and 404

So, today I finally decided to try out IntelliJ IDEA.

After setting up everything I tried to make a VERY simple Spring webMVC Project.

And as this is a Spring application, I'm at a point where I don't have any errors or warnings but just 404 page when I run my project...

Tomcat 9 runs without any warnings or errors and Java seem also to have no problems. Running will open a browser with the way way too common 404 - resource not found error.

SpringTest.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.clomez</groupId>
    <artifactId>SpringTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

HomeController

package com.clomez.controller;

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

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String home(){

        return "home";
    }
}

WebInit.class

package com.clomez.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
public class WebInit  extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

RootConf.class

package com.clomez.config;

import org.springframework.context.annotation.Configuration;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
public class RootConfig {
}

WebConfig.class

package com.clomez.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
@EnableWebMvc
@ComponentScan("com.clomez")
public class WebConfig extends WebMvcConfigurerAdapter{
    @Bean
    public InternalResourceViewResolver resolver(){

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

Please can anybody help me with this? After two months I mostly feel like getting a cancer from this Spring framework thing, but I don't have choice if I want to complete my degree.

Tomcat runs smoothly, there aren't any errors or warnings in the editor, and it brings up a browser with 404 - home.jsp not found.

Upvotes: 3

Views: 1837

Answers (1)

Jon Sampson
Jon Sampson

Reputation: 1551

I've got your code running on Wildfly 10 and after I dropped a home.jsp into my /src/main/webapp/ directory I was served its content.

09:55:10,036 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) 1 Spring WebApplicationInitializers detected on classpath
09:55:10,105 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 69) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/SpringTest-0.0.1-SNAPSHOT'
09:55:10,808 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) Initializing Spring root WebApplicationContext
09:55:10,809 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 69) Root WebApplicationContext: initialization started
09:55:10,818 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Refreshing Root WebApplicationContext: startup date [Fri Apr 14 09:55:10 EDT 2017]; root of context hierarchy
09:55:10,852 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Registering annotated classes: [class com.clomez.config.RootConfig]
09:55:10,962 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 69) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:55:11,018 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 69) Root WebApplicationContext: initialization completed in 209 ms
09:55:11,027 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) Initializing Spring FrameworkServlet 'dispatcher'
09:55:11,027 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 69) FrameworkServlet 'dispatcher': initialization started
09:55:11,030 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Apr 14 09:55:11 EDT 2017]; parent: Root WebApplicationContext
09:55:11,030 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Registering annotated classes: [class com.clomez.config.WebConfig]
09:55:11,116 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 69) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:55:11,270 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 69) Mapped "{[/]}" onto public java.lang.String com.clomez.controller.HomeController.home()
09:55:11,481 INFO  [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 69) HV000001: Hibernate Validator 5.2.4.Final
09:55:11,539 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 69) Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Apr 14 09:55:11 EDT 2017]; parent: Root WebApplicationContext
09:55:11,615 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 69) FrameworkServlet 'dispatcher': initialization completed in 588 ms
09:55:11,616 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 69) WFLYUT0021: Registered web context: /SpringTest-0.0.1-SNAPSHOT
09:55:11,635 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "SpringTest-0.0.1-SNAPSHOT.war" (runtime-name : "SpringTest-0.0.1-SNAPSHOT.war")

When you return "home" in your HomeController, the InternalResourceViewResolver you set up in your WebConfig is used to resolve jsps against the internals or your WAR (where the default base of your war file had /src/main/webapp added to it by maven).

Edit:

And just to confirm for you, I downloaded Tomcat 9.0.0.M19 and I am receiving a 404 when I request http://localhost:8080/SpringTest-0.0.1-SNAPSHOT/. I'll give 8 a quick whirl.

Edit 2:

Tomcat 8.5.13 returns a 404 as well. . . . Both 8.5 and 9 support the Servlet 3 spec, but I'm guessing that there's a wrinkle in there somewhere when it collides with these newer Spring features. I found Spring Java Config: Tomcat deploy without web.xml and http://docs.spring.io/autorepo/docs/spring-framework/4.3.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html notes that:

Mapping to '/' under Tomcat

Apache Tomcat maps its internal DefaultServlet to "/", and on Tomcat versions <= 7.0.14, this servlet mapping cannot be overridden programmatically. 7.0.15 fixes this issue. Overriding the "/" servlet mapping has also been tested successfully under GlassFish 3.1.

I did try changing your mapping from / to /home and Tomcat still wasn't happy (Wildfly was).

More edits to come if I find anything.

Edit 3:

Umm. Well, I'm a bit gobsmacked. Tested with Tomcat 9, instead of requesting http://localhost:8080/SpringTest-0.0.1-SNAPSHOT/ try going to http://localhost:8080/SpringTest/. With a home.jsp, as above, your code works!

Edit 4 (sigh):

For completeness and perhaps just because I'm using Eclipse, I had to add 2 bits of maven build plugin configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

I wouldn't expect these to affect you now if they haven't previously.

Upvotes: 1

Related Questions