访问我的个人博客 秋码个人博客
打开IDEA,创建Springboot项目。
其中pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhenqi</groupId>
<artifactId>study-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_property</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在创建一个Java bean对象。User.java如下:
package com.zhenqi.study.entity;
import java.util.Date;
/**
* Created by wuming on 2017/7/14.
*/
public class User {
private int id;
private String name;
private int age;
private Date birDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirDate() {
return birDate;
}
public void setBirDate(Date birDate) {
this.birDate = birDate;
}
}
最后创建Controller层,IndexController.java如下:
package com.zhenqi.study.controller;
import com.zhenqi.study.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
/**
* Created by wuming on 2017/7/14.
*/
@RestController
@RequestMapping(value = "/index")
public class IndexController {
@Value(value = "${study.secret}")
private String secret;
@Value(value = "${study.number}")
private int id;
@Value(value = "${study.number}")
private int age;
@Value(value = "${study.desc}")
private String desc;
@RequestMapping
public String index() {
return "hello springboot!";
}
// @RequestParam 简单类型的绑定,可以出来get和post
@RequestMapping(value = "/get")
public HashMap<String, Object> get(@RequestParam String name) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "hello zhenqi");
map.put("name", name);
map.put("age",age);
map.put("secret", secret);
map.put("id", id);
map.put("desc", desc);
return map;
}
// @PathVariable 获得请求url中的动态参数
@RequestMapping(value = "/get/{id}/{name}/{age}")
public User getUser(@PathVariable int id, @PathVariable String name,@PathVariable int age) {
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setBirDate(new Date());
return user;
}
}
其配置文件 application.properties
#32位随机字符串
study.secret=${random.value}
#int类型的随机数字
study.number=${random.int}
study.name=zhenqi-study
#属性占位符属性
study.desc=${study.name} is a domain name
#应用端口
server.port=8090
#时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#时区设置
spring.jackson.time-zone=Asia/Shanghai
run 这个类。
浏览器访问效果如下: