SpringBoot @RestController注解下返回html
时间: 2023-08-18 20:12:51 浏览: 139
在SpringBoot中,使用@RestController注解的Controller方法默认返回的是JSON、XML或自定义mediaType内容,而不是HTML页面。如果需要返回HTML页面,可以使用@Controller注解配合视图解析器InternalResourceViewResolver来实现。首先,在SpringBoot的默认配置文件application.yml中添加地址修饰,指定HTML页面的前缀和后缀。例如,可以在application.yml中添加以下配置:
```
spring:
mvc:
view:
prefix: /
suffix: .html
```
然后,在Controller类中使用@Controller注解,并在对应的方法上不加@ResponseBody注解。例如:
```java
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
}
```
这样,当访问"/hello"路径时,返回的内容将会是hello.html页面的内容。
#### 引用[.reference_title]
- *1* *3* [springboot注解@RestController](https://blog.csdn.net/matthewchen123/article/details/113181836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Springboot注解@RestController](https://blog.csdn.net/e_mmm0629/article/details/109038732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文