具有基本Spring 4 MVC Rest应用程序的HTTP状态404

时间:2016-11-09 21:13:50

标签: java spring spring-mvc

我正在尝试按照基本教程启动并运行我的应用程序,在此处找到:http://websystique.com/springmvc/spring-4-mvc-rest-service-example-using-restcontroller/

目前我有以下课程:

AppConfig.java:

package com.cgi.miam.config;

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;

@Configuration
@ComponentScan("com.cgi.miam")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {

}

WebAppInitializer.java:

package com.cgi.miam.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*" };
    }
}

MemberController.java:

package com.cgi.miam.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

    @RequestMapping("/")
    public String welcome() {// Welcome page, non-rest
        return "Welcome to RestTemplate Example.";
    }

    @GetMapping("/person/{id}")
    public ResponseEntity getCustomer(@PathVariable("id") Integer id) {

        return new ResponseEntity("test", HttpStatus.OK);
    }
}

我的pom.xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cgi.miam</groupId>
    <artifactId>miamWebApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>miamWebApp Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b01</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.4</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>SpringRest</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</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>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>MemberInformation</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

我已经在Eclipse中的Tomcat 9.0中部署了它,并且它成功启动了。但是,如果我转到http://localhost:8080/MemberInformation/http://localhost:8080/MemberInformation/personhttp://localhost:8080/MemberInformation/person/2,我总会收到404。似乎所有教程都可以在此时启动并获取信息,但在这里似乎并不起作用。会员信息&#39;我浏览http://localhost:8080/manager时似乎部署了应用程序。有什么提示吗?

2 个答案:

答案 0 :(得分:0)

问题似乎是Spring无法找到您的RestController。在@Configuration中,请注意您需要如何指定基本包。试试这个:

@ComponentScan(basePackages = "com.cgi.miam") //will look into com.cgi.miam.controller as well

而不是..

@ComponentScan("com.cgi.miam")

答案 1 :(得分:0)

似乎我的问题在于我部署web服务的方式。 pom.xml中的以下部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <warName>MemberInformation</warName>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>

导致war文件部署到该位置,即使应用程序已添加到Eclipse中的Tomcat服务器,也未部署。解决方法是注释掉pom的这一部分,或者将生成的war文件传输到Tomcat Webapps文件夹。