转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80404645
本文出自【赵彦军的博客】
文章目录
用户模型类
package com.yiba.wifi.news.bean.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
Integer id;
String name;
Integer age ;
//....set 省略
//....get 省略
}
Get 请求
1、无参
请求api
http://localhost:8083/api/find
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 查询 id 数据
* @return
*/
@GetMapping("find")
public User findOne() {
//查询用户逻辑.....
return new User();
}
}
2、带参数
请求api
http://localhost:8083/api/find?name=zhaoyanjun
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 查询 id 数据
* 当 name 为 null 的时候,用默认值 yanjun 代替
* @return
*/
@GetMapping("find")
public User findOne(@RequestParam(value = "name", defaultValue = "yanjun") String name) {
//查询用户逻辑.....
logger.info("name:" + name);
return new User();
}
}
3、RESTful API
请求api
http://localhost:8083/api/find/5
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 查询 id 数据
*
* @param id
* @return
*/
@GetMapping("find/{id}")
public User findOne(@PathVariable("id") Integer id) {
logger.info("id:" + id);
//查询用户逻辑.....
return new User();
}
}
POST 请求
1、表单请求
1、 请求api 方式一
http://localhost:8083/api/find?name=zhaoyanjun
实例图:
2、 请求api 方式二:表单
http://localhost:8083/api/find
实例图:
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 查询 id 数据
*
* @return
*/
@PostMapping("find")
public User findOne(@RequestParam(value = "name", defaultValue = "yanjun",required = false) String name) {
//查询用户逻辑.....
logger.info("name:" + name);
return new User();
}
}
2、参数为对象
请求api
http://localhost:8083/api/find
请求 body
{
"id": 1,
"name": "yanjun",
"age": 18
}
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 查询 id 数据
*
* @return
*/
@PostMapping("find")
public User findOne(@RequestBody User user) {
//查询用户逻辑.....
logger.info("name:" + user.getName());
return user;
}
}
请求示例:
请求 header 获取
获取单个 header
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
*
* @param user
* @param token 获取 header 里面的 token 字段
* @return
*/
@PostMapping("find")
public User findOne(@RequestBody User user,
@RequestHeader(value = "token") String token) {
//查询用户逻辑.....
logger.info("token:" + token);
return user;
}
}
请求示例
获取所有 header
接口设计
package com.yiba.wifi.news.controller;
import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("api")
public class UserController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private HttpServletRequest request;
@PostMapping("find")
public User findOne(@RequestBody User user) {
logger.info("CONTENT_TYPE:" + request.getHeader(HttpHeaders.CONTENT_TYPE)); //获取header
logger.info("TOKEN:" + request.getHeader("token")); //获取header
return user;
}
}
SpringBoot 上传多文件 MultipartFile
服务器接收一个文件
@RestController
public class UserController {
@PostMapping("find")
public String findOne(@RequestParam("file") MultipartFile part) {
//part 名字
String fileName = part.getName();
//文件名字
String originalFilename = part.getOriginalFilename();
//获取文件内容
String fileContent = getResult(part);
return "成功";
}
//读取文件内容
public String getResult(MultipartFile part) {
BufferedInputStream bufferedInputStream = null;
try {
InputStream inputStream = part.getInputStream();
bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1024];
int length; //代表实际读取的字节数
String result = "";
while ((length = bufferedInputStream.read(buffer)) != -1) {
//length 代表实际读取的字节数
result = result + new String(buffer, 0, length);
}
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
}
服务器接收多个文件,方式一
@RestController
public class UserController {
@PostMapping("find")
public String findOne(@RequestParam("file") MultipartFile part, @RequestParam("file2") MultipartFile part2) {
//获取第一个文件内容
String fileContent = getResult(part);
//获取第二个文件内容
String fileContent2 = getResult(part2);
return "成功";
}
}
服务器接收多个文件,方式二
@RestController
public class UserController {
@PostMapping("find")
public String findOne(@RequestParam("file") MultipartFile[] files) {
//接收多个文件
for (MultipartFile part : files) {
//获取文件内容
String fileContent = getResult(part);
System.out.printf("内容:" + part.getName() + " " + fileContent);
}
return "成功";
}
}
服务器接收多个文件+单个普通参数
@RestController
public class UserController {
@PostMapping("find")
public String findOne(@RequestParam("file") MultipartFile[] files, @RequestParam String age) {
//接收多个文件
for (MultipartFile part : files) {
//获取文件内容
String fileContent = getResult(part);
System.out.printf("内容:" + part.getName() + " " + fileContent);
}
//接收普通参数
String ageValue = age;
System.out.printf("普通参数age:" + ageValue);
return "成功";
}
}
服务器接收多个文件+多个普通参数
@RestController
public class UserController {
@PostMapping("find")
public String findOne(@RequestParam("file") MultipartFile[] files, @RequestParam String age, @RequestParam String name) {
//接收多个文件
for (MultipartFile part : files) {
//获取文件内容
String fileContent = getResult(part);
System.out.printf("内容:" + part.getName() + " " + fileContent);
}
//接收普通参数
String ageValue = age;
System.out.printf("普通参数age:" + ageValue);
return "成功";
}
}
解决网站跨域问题
添加 @CrossOrigin(origins = "*")
注解就可以解决
如下:
@RestController
public class MyCro {
@PostMapping("/getUser")
@CrossOrigin(origins = "*")
public void run(){
}
}
个人微信号:zhaoyanjun125 , 欢迎关注