具有多个控制器的Spring Boot API?

use*_*412 11 java model-view-controller spring-mvc gradle spring-boot

我开始学习Spring Boot了.我正在努力找到一个包含多个RestControllers的例子,它向我表明我可能做错了什么.我正在尝试一个非常简单的例子:目标是进行如下调用:

localhost:8080/
localhost:8080/employees/bob
localhost:8080/departments
Run Code Online (Sandbox Code Playgroud)

我只能得到localhost:8080 /来显示.其他调用返回响应:此应用程序没有/ error的显式映射,因此您将此视为回退.

com.demo.departments
Department.java
DepartmentController.java

com.demo.employees
Employee.java
EmployeeController.java

com.demo
BootDemoApplication.java
Run Code Online (Sandbox Code Playgroud)

码:

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>";
}
Run Code Online (Sandbox Code Playgroud)

Vie*_*tDD 14

我正在尝试Spring Boot并遇到同样的问题,并且修复了它,我在这里发布我的解决方案,因为我觉得它对某些人有帮助.

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

com.example.demo
              |
              +-> controller
              |      |
              |      +--> IndexController.java
              |      +--> LoginController.java
              |
              +-> Application.java
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Spring将扫描演示包的子包的所有组件

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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


use*_*412 7

显然,在主类中使用@springbootApplication表示法无法看到不同包中的控制器.这里解释的解决方案,https://kamwo.me/java-spring-boot-mvc-ontroller-not-called/.


Jan*_*ena 5

确保 @SpringBootApplication 类所在的包的级别高于包含 @RestControllers 的所有其他包,或者位于同一包中。