@restcontrollerhe@controller
时间: 2024-02-13 15:57:10 浏览: 73
@RestController和@Controller的区别在于@RestController注解相当于@ResponseBody + @Controller合在一起的作用,即@RestController是用来返回JSON数据的,而@Controller一般用来返回视图页面。下面是两个注解的示例代码:
1.使用@Controller注解
```java
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/world")
public String helloWorld(Model model) {
model.addAttribute("message", "Hello World!");
return "hello";
}
}
```
2.使用@RestController注解
```java
@RestController
@RequestMapping("/hello")
public class HelloRestController {
@RequestMapping("/world")
public Map<String, String> helloWorld() {
Map<String, String> map = new HashMap<>();
map.put("message", "Hello World!");
return map;
}
}
```
阅读全文