一、@RestController和@Controller的区别

  • @RestController注解相当于@ResponseBody + @Controller合在一起的作用。
  1. 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。例如:本来应该到success.jsp页面的,则其显示success.
  2. 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
  3. 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

二、ConfigurationProperties配置方式

  2.1  配置一个MySqlProperties

package com.shyroke.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "mysql")
public class MySqlProperties {
private String jdbcName; private String dbUrl; private String userName; private String password; public String getJdbcName() {
return jdbcName;
} public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
} public String getDbUrl() {
return dbUrl;
} public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

  2.2  配置application.properties  

server.port=8088
welcome=spring boot \u4F60\u597D mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=

  2.3  编写控制器

package com.shyroke.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.shyroke.config.MySqlProperties; @RestController
@RequestMapping(value="/hello")
public class HelloWorldController { @Value("${welcome}")
private String welcome; @Autowired
private MySqlProperties MySqlProperties; @RequestMapping(value="/login")
public String login() {
return "mysql.jdbcName:"+MySqlProperties.getJdbcName()+"<br/>"
+"mysql.dbUrl:"+MySqlProperties.getDbUrl()+"<br/>"
+"mysql.userName:"+MySqlProperties.getUserName()+"<br/>"
+"mysql.password:"+MySqlProperties.getPassword();
}
}

  2.4  结果

(五)Spring Boot之@RestController注解和ConfigurationProperties配置多个属性的更多相关文章

  1. 读懂这些spring boot的核心注解,快速配置完成项目搭建

    在spring boot中,摒弃了spring以往项目中大量繁琐的配置,遵循约定大于配置的原则,通过自身默认配置,极大的降低了项目搭建的复杂度.同样在spring boot中,大量注解的使用,使得代码 ...

  2. spring boot 的常用注解使用 总结

    附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...

  3. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  4. Spring Boot Web 开发注解篇

    本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...

  5. Spring boot 使用的注解有哪些?

    Spring boot 使用的注解有哪些? 注解 作用 @SpringBootApplication 等价于 @Configuration + @EnableAutoConfiguration + @ ...

  6. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  7. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  8. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  9. Spring Boot中@Scheduled注解的使用方法

    Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...

随机推荐

  1. bad object refs/remotes/origin/HEAD

    How to handle git gc fatal: bad object refs/remotes/origin/HEAD error: failed to run repack I random ...

  2. python文件导出exe可执行程序

    开门见山的说: 1.安装pyinstaller.(windows 用pip3 Mac 用pip)在cmd中输入:pip3 install pyinstaller 2.找到你要打包的文件的目录的上一个目 ...

  3. https://en.wikipedia.org/wiki/Green_threads

    https://en.wikipedia.org/wiki/Green_threads

  4. SpringBoot2.X中的静态资源访问失效

    今天开始使用SpringBoot写项目,于是先让其能够访问静态资源,但是配置半天也不能访问我的helloWorld页面,原来,在SpringBoot2.x中,一下静态资源路径不生效了. classpa ...

  5. gitment初始化评论跳回博客首页

    表现 众所周知,gitment评论系统需要初始化以创建对应的issue,可是我在点击login with github的时候,总是跳向博客首页!WTF!什么鬼?这样不程序啊? 排查 1.F12查看lo ...

  6. Mac下安装php-memcached扩展

    [libmemcached安装] libmemcached可以通过直接下载后解压也可以采用wget下载 先下载libmemcached:方式一:libmemcached下载地址:https://lau ...

  7. Qt kdChart 甘特图案例

    KDChart  甘特图在Qt中的加载使用案例,代码来自官方 mainwindow.h /******************************************************* ...

  8. 123457123456---com.threeObj3.BabyShizi02--- 宝宝识字02

    com.threeObj3.BabyShizi02--- 宝宝识字02

  9. 关于做移动端ui自动化测试使用PC代理网络会出现的问题

    无论是手动操作app还是ui脚本操作app,只要你使用了代理网络,第一次请求的时长会耗时特别长,所以在移动端测试时尽量不要使用代理网络,减少不必要的麻烦

  10. ansible软件部署

    每一个软件或集群都创建一个单独的角色. mkdir -pv ansible/roles/{jdk,hadoop,spark}/{tasks,files,templates,meta,handlers, ...