一、@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. APP测试面试题(一)

    一.开场问题:(自由发挥) 1.请自我介绍一下: 2.为什么离开上一个公司呢? 3.做测试多久了?以前做过哪些项目?你们以前测试的流程是怎样的?用过哪些测试工具? 4.你觉得为什么要在一个团队中开展软 ...

  2. matlab学习——05插值和拟合(黄河小浪底调水调沙问题)

    05插值和拟合 黄河小浪底调水调沙问题 data3.txt 1800 1900 2100 2200 2300 2400 2500 2600 2650 2700 2720 2650 32 60 75 8 ...

  3. Windows server 2008 R2 桌面服务器管理器打开提示:试图引用不存在的令牌

    来源:https://social.technet.microsoft.com/Forums/zh-CN/90f376a3-2b52-46c1-be34-4a2dbf4fdea2/winserver2 ...

  4. scala的wordcount

    import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.SparkSession object Wo ...

  5. react 组装table列表带分页

    2.组装编辑界面 /** * Created by hldev on 17-6-14. */ import React, {Component} from "react"; imp ...

  6. galera集群启动异常问题

    WSREP: failed to open gcomm backend connection: 131: invalid UUID 进入该数据库节点/var/lib/mysql/目录,将文件gvwst ...

  7. 最新 开创java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.开创等10家互联网公司的校招Offer,因为某些自身原因最终选择了开创.6.7月主要是做系统复习.项目复盘.LeetCode ...

  8. JS的BOM操作语法

    整理了一下JS的BOM操作语法,这里记录一下. <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  9. APP 和小程序中通过日期格式获取时间戳的一个bug

    介绍一下背景:业务逻辑就不多说了,就说关键出问题的一步,需要将 2019-10-10 这个格式转换为时间戳.在不同平台不同场景下问题还很怪异 app上:ios 安卓线上的都有问题  ios模拟器没问题 ...

  10. Win7 JavaEE 安装

    新建四个目录 D:\ApacheServer\eclipse 存放eclipse D:\ApacheServer\jdk jdk安装目录 D:\ApacheServer\apache-tomcat 存 ...