Spring Boot API如何使用多个控制器?

21

我开始学习Spring Boot,但是我很难找到一个具有多个RestControllers示例的例子,这让我认为我的做法可能有问题。我正在尝试一个非常简单的例子:目标是使以下调用:

localhost:8080/
localhost:8080/employees/bob
localhost:8080/departments

我只能显示localhost:8080/。其他调用返回响应:此应用程序没有显式映射 /error,因此您看到这是一个回退。

com.demo.departments
Department.java
DepartmentController.java

com.demo.employees
Employee.java
EmployeeController.java

com.demo
BootDemoApplication.java

代码:

package com.demo.departments
@RestController
@RequestMapping("/departments")
public class DepartmentController {


@RequestMapping("")
public String get(){
    return "test..";

}

@RequestMapping("/list")
public List<Department> getDepartments(){
    return null;

}

}
--------------------------------------------------------------------
package com.demo.employees
@RestController
@RequestMapping("/employees")
public class EmployeeController {

Employee e =new Employee();

@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
public Employee getEmployeeInJSON(@PathVariable String name) {

 e.setName(name);
 e.setEmail("employee1@genuitec.com");

 return e;

}
}
-----------------------------------------------------------------------

package com.demo
@RestController
@SpringBootApplication

public class BootDemoApplication {

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

@RequestMapping("/")
String home(){
    return "<html> This is the home page for Boot Demo.</html>";
}

这应该可以工作(已测试)。但是你没有为 http://localhost:8080/ 提供任何控制器,所以错误可能在那里。 - g00glen00b
1
您正在返回JSON,但未请求它(浏览器期望HTML),因此找不到处理请求的方法。 - M. Deinum
你在日志或控制台中看到其他错误吗?如果你通过gradle运行,请尝试添加“-i”选项。 - Nikem
@user1529412,你的示例可行...我创建了一个新的Spring Boot项目,添加了spring-boot-starter-web,并且只复制粘贴了你上面提到的类。它可以正常工作。 - g00glen00b
@g00glen00b,我很好奇,你是怎么运行这段代码的?我将它导入到Eclipse中,并作为Spring Boot项目运行,但仍然存在同样的问题。我认为可能是Eclipse本身的问题。 - user1529412
显示剩余6条评论
8个回答

27

我正在尝试使用Spring Boot,并遇到了同样的问题,刚刚解决了。我在这里发布我的解决方案,因为我认为它可能对某些人有帮助。

首先,将应用程序类(包含main方法)放在控制器包的根目录下:

com.example.demo
              |
              +-> controller
              |      |
              |      +--> IndexController.java
              |      +--> LoginController.java
              |
              +-> Application.java

Application.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Spring会扫描demo包的子包中的所有组件。

IndexController.java(返回index.html视图)

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = {""})
public class IndexController {

    @GetMapping(value = {""})
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }

}

LoginController.java(返回login.html视图)

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = {"/login"})
public class LoginController {
    @GetMapping(value = {""})
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }
}

现在我可以进入首页视图: http://localhost:8080/demo/ 和 登录视图: http://localhost:8080/demo/login


13

5

请确保 @SpringBootApplication 类在一个包中,该包位于包含所有 @RestControllers 的其他包的上一级,或者与该包相同。


4

对于Spring-boot 1.3.x及以上版本,将基础包传递给SpringBootApplication应该可以工作:

@SpringBootApplication(scanBasePackages = {"com.demo"})
public class DemoBootApplication {
    // code
}

我在一个类似的应用中使用spring-boot 1.4.0时,这个方法对我有效。但是对于早期版本的spring-boot来说,似乎你需要放弃使用SpringBootApplication,并改为使用以下内容以获得与上述相同的效果:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.demo"})
public class DemoBootApplication {
    // code
}

我在这篇博客文章的评论中发现了这个问题。


1

ComponentScan注解在大多数情况下都是有效的。

请参考以下示例,您可以应用类似的方法。
package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@ComponentScan(basePackages = {"com.demo"})
@SpringBootApplication
public class DemoApplication {

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

0

我不确定这是否是正确的方法,但是当我将我的第二个控制器注释从 @Controller 更改为 @RestController 时,它开始工作。


0

试试这个

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {

        Object[] sources = new Object[2];
        sources[0] = Controller1.class;
        sources[1] = Controller2.class;
        SpringApplication.run(sources, args);
    }

}

-1

请尝试以下:

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class BootDemoApplication {

public static void main(String[] args) {

    SpringApplication.run(BootDemoApplication.class);
}
}

@RestController
@RequestMapping(value = "test", produces =    MediaType.APPLICATION_JSON_VALUE)
public class TestController {

@RequestMapping(method = RequestMethod.GET)
public String test() {
    return "from test method";
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接