HTTP状态404?

时间:2017-09-24 19:51:52

标签: java spring rest maven spring-mvc

Directory

的AppConfig

package com.example.configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.SpringTest")
public class AppConfig {


    }

AppConfigInitialisation.java ##

    package com.example.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppConfigInitialization  extends AbstractAnnotationConfigDispatcherServletInitializer {
     @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { AppConfig.class };
        }

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

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

User.java

package com.example.controller;
import com.example.model.*;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
     @RequestMapping("/")
        public String welcome() {//Welcome page, non-rest
            return "Welcome to RestTemplate Example.";
        }

        @RequestMapping("/hello/{player}")
        public User user(@PathVariable String player) {//REST Endpoint.

            User msg = new User(player, "Hello " + player);
            return msg;

        }

}

UserController.java

    package com.example.controller;
import com.example.model.*;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
     @RequestMapping("/")
        public String welcome() {//Welcome page, non-rest
            return "Welcome to RestTemplate Example.";
        }

        @RequestMapping("/hello/{player}")
        public User user(@PathVariable String player) {//REST Endpoint.

            User msg = new User(player, "Hello " + player);
            return msg;

        }

}

的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>SpringTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.3.0.RELEASE</springframework.version>
        <jackson.library>2.7.5</jackson.library>
  </properties>

  <dependencies>


     <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.library}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.library}</version>
        </dependency>
  </dependencies>
</project>

我收到“说明请求的资源不可用。”我想在tomcat v8.0服务器上运行它。即使我从站点下载项目spring项目并将它们作为maven项目导入,我也会遇到同样的问题。帮帮我

2 个答案:

答案 0 :(得分:0)

您可能需要在类型级别(在UserController之上)添加@RequestMapping(“/”)。

答案 1 :(得分:0)

您将组件扫描基础包作为&#34; com.example.SpringTest&#34;。但UserController在com.example.controller