在idea中生成spring-boot工程:
自动注入数据:
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
@Controller注释表示返回的是html页面或者jsp页面
@ResponseBody作用在方法上表示返回的是json格式的数据并写在http response body的请求响应中,常用于异步请求
自动注入数据:
写一个对象类用@Component、@PropertySource("此处写要注入数据的文件")、
@ConfigurationProperties("prefix="+"")(使要注入的类和要注入的数据相对应)
然后在Controller里用@Autowired来注释要注入的类,如果要注入的数据中有中文存在需要
点击file中new project中的Spring Initializr在技术使用到的项目中选择web.生成项目后可以在项目自动生成的application.properties文件中写入
server.tomcat.uri.ecoding=UTF-8spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=utf-8
五个配置文件,然后在setting中的file encoding里的Properties下的两项中选为UTF-8和打钩来控制自动注入的数据中文不会乱码
@RestController注释表示返回就是return里的东西,controller中的方法无法返回jsp页面或者html.配置的是视图不起作用.@Controller注释表示返回的是html页面或者jsp页面
@ResponseBody作用在方法上表示返回的是json格式的数据并写在http response body的请求响应中,常用于异步请求
@RequestBody将http请求正文插入方法中,适合将请求体直接写入某个对象中去.
可以在配置文件中创建多个application-*.properties来决定访问的环境和端口
日志配置:
配置日志输出位置
logging.file=/home/sang/workspace/log.log
配置日志输出级别
logging.level.org.springframework.web=debug