Spring Boot API with Multiple Controllers?

2020-06-03 00:22发布

I am starting to learn Spring Boot. I am struggling to find an example with multiple RestControllers, which indicates to me that I may be doing something wrong. I am trying a very simple example: The goal is to make calls like the following:

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

I can only get localhost:8080/ to display. The other calls return response: This application has no explicit mapping for /error, so you are seeing this as a fallback.

com.demo.departments
Department.java
DepartmentController.java

com.demo.employees
Employee.java
EmployeeController.java

com.demo
BootDemoApplication.java

Code:

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

8条回答
兄弟一词,经得起流年.
2楼-- · 2020-06-03 00:49

Make sure that the @SpringBootApplication class is in a package which is a level above all other packages that contain @RestControllers, or in the same package.

查看更多
神经病院院长
3楼-- · 2020-06-03 00:56

Apparently Controllers in different packages can't be seen with @springbootApplication notation in the main class. The solution explained here, https://kamwo.me/java-spring-boot-mvc-ontroller-not-called/.

查看更多
forever°为你锁心
4楼-- · 2020-06-03 00:56

Try this

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

}
查看更多
闹够了就滚
5楼-- · 2020-06-03 00:58

I'm not sure if this is the right way to do it, but when I changed my 2nd Controllers annotation from @Controller to @RestController it started working.

查看更多
Luminary・发光体
6楼-- · 2020-06-03 01:02

ComponentScan annotation works in most cases.

See below example, you could apply similar.
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);
}
}
查看更多
孤傲高冷的网名
7楼-- · 2020-06-03 01:06

Try below:-

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

}
查看更多
登录 后发表回答