2016-05-22 19 views
6

我開始學習Spring Boot。我正在努力尋找一個具有多個RestControllers的例子,這表明我可能做錯了什麼。我正在嘗試一個非常簡單的例子:目標是撥打電話,如下所示:帶有多個控制器的Spring Boot API?

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

我只能得到localhost:8080 /來顯示。其他調用返回響應:此應用程序沒有顯式映射/錯誤,因此您將此視爲後備。

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("[email protected]"); 

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

我不認爲你需要在頂級控制器映射上的前面的「/」。 – ChiefTwoPencils

+0

我試過了,沒有什麼區別。 – user1529412

+0

這應該工作(測試它)。儘管你沒有爲'http:// localhost:8080 /'提供任何控制器,所以也許錯誤在那裏。 – g00glen00b

回答

0

以下嘗試: -

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

} 
0

春天引導1.3.x中和起來,路過一個基礎包到SpringBootApplication應該工作:

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

這爲我工作使用彈簧啓動1.4.0類似的應用程序。對於早期版本的春天啓動的,看來你必須放棄使用SpringBootApplication,而是使用以下方法來獲得與上述同樣的效果:

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

我發現這個在評論這一blog post

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

你對該代碼有解釋嗎? –

0

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

我想春天引導並得到了同樣的問題,只是固定它,我在這裏發佈我的解決方案,因爲我覺得它的人也許有幫助。

首先,將應用程序類(包含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將掃描的所有組件分包演示

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(返回登錄。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

1

確保@SpringBootApplication類是在一個包是所有其他包以上的水平包含@RestControllers,或在同一個包中。

相關問題