目录
三、SpringBoot基础HTTP接口POST,PUT,DELETE请求实战
一、springboot项目搭建
1、手动创建
开发环境
jdk 1.8
Spring Boot :2.1.1.RELEASE
Maven:3.6.3
开发工具:eclipse
以上环境配置好,插件安装上
1、创建项目
2、项目创建后的目录结构
3、项目创建后,启动类和pom.xml自动创建
4、注解
@SpringBootApplication是一个复合注解,记录下重要的三个
@SpringBootConfiguration:复合注解,其中最重要的是@Configration指明该类由Spring容器管理
@EnableAutoConfiguration:该注解用于启动服务自动配置功能
@ComponentScan:该注解用于扫描类,其作用类似于Spring中的<xontext:component-scan>标签。
@RestController注解,复合注解,其中比较重要的
@Controller和@ResponseBody,指定controller返回的对象自动转化为json格式并返回(基本类型及包装类、String除外)
<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>net.sd</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
5、Controller
@RestController
public class SampleController {
@RequestMapping("/hello")
public String HelloWord() {
return "Hello World";
}
}
2、快速自动创建
推荐自动创建,比较规范
二、SpringBoot基础HTTP接口GET请求实战
1、@RequestMapping(path = "/{id}/{host}",method = RequestMethod.GET)
public int user(@PathVariable int id,@PathVariable String host) {
2、@GetMapping(path = "/user/{user_id}")
public Object getUser(@PathVariable("user_id") String userId, @RequestParam("NAME") String name ,@RequestHeader String uuid) {
@RestController
public class GetController {
@RequestMapping(path = "/{id}/{host}",method = RequestMethod.GET)
public int user(@PathVariable int id,@PathVariable String host) {
return id;
}
@GetMapping(path = "/user/{user_id}")
public Object getUser(@PathVariable("user_id") String userId, @RequestParam("NAME") String name ,@RequestHeader String uuid) {
Map<String,String> param = new HashMap<String, String>();
param.put("userId", userId);
param.put("name", name);
param.put("uuid", uuid);
return param;
}
}
三、SpringBoot基础HTTP接口POST,PUT,DELETE请求实战
@PostMapping = @RequestMapping(method = RequestMethod.POST)//提交Post提交
@PutMapping = @RequestMapping(method = RequestMethod.PUT)//更新Put提交
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)//删除Delete提交
四、常用Json框架
1、常用框架 阿里 fastjson,谷歌gson等
JavaBean序列化为Json,性能:Jackson > FastJson > Gson > Json-lib 同个结构
Jackson、FastJson、Gson类库各有优点,各有自己的专长
空间换时间,时间换空间
2、jackson处理相关自动
指定字段不返回:@JsonIgnore
指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
空字段不返回:@JsonInclude(Include.NON_NUll)
指定别名:@JsonProperty
@RestController
public class SampleController {
@RequestMapping(path = "/jsonUser",method = RequestMethod.POST)
public User jsonUser(@RequestBody User user) {
user.setCreateDate(new Date());
user.setPhone("1234");
return user;
}
}
public class User {
private int age;
@JsonProperty("ss")
private String name;
@JsonIgnore
private String phone;
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
private Date createDate;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public User(int age, String name, String phone, Date createDate) {
super();
this.age = age;
this.name = name;
this.phone = phone;
this.createDate = createDate;
}
}