@RequestMapping不重定向到指定的URL:Spring Boot应用程序

时间:2017-04-14 07:10:06

标签: spring spring-mvc spring-boot

我正在尝试创建一个可以连接到postgres的示例Spring启动应用程序。但我无法重定向到其余控制器中指定的路径。我的项目代码如下: 的pom.xml

    

http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vmware.springboot</groupId>
    <artifactId>SpringBootSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootDemo1</name>
    <description>Sample project for Spring Eureka</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

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

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

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


</project>

application.properties

server.port=3000

spring.datasource.url= jdbc:postgresql://localhost:5432/local_db
spring.datasource.data-username=postgres
spring.datasource.data-password=postgres

spring.jpa.hibernate.ddl-auto=create-drop

SpringBootExampleApplication.java

package org.kumar.springboot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import org.kumar.spring.enitiy.Employee;

@SpringBootApplication
@ComponentScan
public class SpringBootExampleApplication {


    public static void main(String[] args) {


        SpringApplication.run(SpringBootExampleApplication.class, args);
    }
}

EmployeeController.java

package org.kumar.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.kumar.spring.enitiy.Employee;
import org.kumar.spring.repository.EmployeeRepository;

@RestController
@RequestMapping("/data")
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    @RequestMapping("/employees")
    public List<Employee> getEmployees(){
        return (List<Employee>)employeeRepository.findAll();
    }


}

Employee.java

package org.kumar.spring.enitiy;

public class Employee {

    private String employeeId;
    private String employeeName;

    public String getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    @Override
    public String toString() {
        return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + "]";
    }
    public Employee(String employeeId, String employeeName) {
        super();
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }

}

EmployeeRepository.java

package org.kumar.spring.repository;

import org.springframework.data.repository.CrudRepository;

import org.kumar.spring.enitiy.Employee;

public interface EmployeeRepository extends CrudRepository<Employee, String>
{

}

我正在尝试运行以下内容: 请求:获取http://localhost:3000/data/employees 回复:

 {
  "timestamp": 1492152367659,
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/data/employees"
}

我犯了什么错误。

1 个答案:

答案 0 :(得分:4)

基本上,您的EmployeeController类已被<容器检测/扫描,因为这些类位于不同的包层次结构中且容器已包含没有被指示扫描要查找的包。

因此有两种方法可以解决这个问题:

(1)将您的SpringBootExampleApplication课程包改为org.kumar.spring

(2)添加@ComponentScan(basePackages = {"org.kumar.spring"})

我建议您更喜欢上面的选项(1),对于选项(2),您需要添加@EnableJpaRepositories以使程序从头到尾工作(即使用您的{连接到数据库) {1}}类)。