【问题标题】:Spring Boot API with Multiple Controllers?具有多个控制器的 Spring Boot API?
【发布时间】:2016-09-19 03:37:23
【问题描述】:

我开始学习 Spring Boot。我正在努力寻找具有多个 RestController 的示例,这向我表明我可能做错了什么。我正在尝试一个非常简单的示例:目标是进行如下调用:

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/ 提供任何控制器,所以可能是错误所在。
  • 您正在返回 JSON,但没有请求它(浏览器需要 HTML),因此找不到处理请求的方法。
  • 是的,员工是 POJO。它没有调用调试器中其他 2 个类中的任何代码。它抱怨的是映射,而不是结果。

标签: java spring-mvc model-view-controller gradle spring-boot


【解决方案1】:

我正在尝试 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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      对于 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
      }
      

      我在 blog post 的 cmets 中找到了这个。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          见下面的例子,你可以应用类似的。
          包 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);
          }
          }
          

          【讨论】:

            【解决方案6】:

            试试这个

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

                试试下面:-

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-07-04
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-03-14
                  • 2018-10-04
                  • 2020-11-06
                  • 2016-09-17
                  相关资源
                  最近更新 更多