springboot Restful使用记录

创建项目

通过spring官网创建项目
https://start.spring.io/

在这里插入图片描述

  1. 项目名称取为studyRest
  2. 项目依赖WEB

Rest组件使用

  • 使用@RestController标记类为提供Restful服务的Contoller
  • @GetMapping为资源定位一部分,也就是url,对应http://localhost:8080/test
@RestController
public class MyRestContoller1 {
	@GetMapping("/test")
	public Map<String, String> getData() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", "111");
		data.put("name", "zhangsan");
		return data;
	}
}

测试(这里使用浏览器测试,后续使用Postman工具)
在这里插入图片描述
@GetMapping关键字对应GET请求,也就是查询,请求还可以有参数,对应@PathVariable@RequestParam注解

	@GetMapping("/test/{id}")
	public Map<String, String> getData2(@PathVariable String id, @RequestParam(required = false) String name) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", id);
		data.put("name", name);
		return data;
	}

测试,返回值为入参传入参数
在这里插入图片描述
Post类型,新增操作

  • 新增使用@PostMapping描述URL
  • 新增一般都会带有大量数据,一般都是使用@RequestBody注解封装参数
	@PostMapping("/test2/add")
	public Map<String, String> addData(@RequestBody Map<String, String> data) {
		return data;
	}

测试
在这里插入图片描述
注意两点,不正确都会报错

  1. 请求类型必须是POST
  2. Content-type必须要设置为application/json,因为入参形式为JSON格式

在这里插入图片描述
更新与删除操作
使用上与Post一致,只是不同类型需要使用对应的主机

  • PUT : @PutMapping
  • DELETE: @DeleteMapping
	@PutMapping("/test2/update")
	public Map<String, String> updateData(@RequestBody Map<String, String> data) {
		return data;
	}
	
	@DeleteMapping("/test2/delete")
	public Map<String, String> deleteData(@RequestBody Map<String, String> data) {
		return data;
	}
RequestMapping使用

RequestMapping是一个通用注解,包含上述所有操作

@RestController
@RequestMapping(value = "/parent")
public class RequestRestContoller {
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Map<String, String> get() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", "111");
		data.put("name", "zhangsan");
		return data;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, String> add() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", "111");
		data.put("name", "zhangsan");
		return data;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public Map<String, String> update() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", "111");
		data.put("name", "zhangsan");
		return data;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Map<String, String> delete() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", "111");
		data.put("name", "zhangsan");
		return data;
	}
}

上述还有贴在class上面的注解:@RequestMapping(value = "/parent"),如果是class上面的注解,那么方法上面的url需要加上class上面的注解
如:http://localhost:8080/parent/get或http://localhost:8080/parent/add

其中可以属于请求参数和响应数据类型

@RequestMapping(value = "/parent", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

其中consumes 约束入参类型,produces 约束响应数据类型

测试Content-Type:text/plain报错,由于设置了JSON格式
在这里插入图片描述

支持哪些格式参考Media定义

org.springframework.http.MediaType

XML格式数据支持

这里扩展一下,返回XML格式数据

引入XML依赖包

	<dependency>
		<groupId>com.fasterxml.jackson.dataformat</groupId>
		<artifactId>jackson-dataformat-xml</artifactId>
	</dependency>

测试类

@RestController
public class DataRestContoller {
	@RequestMapping(value = "/addJsonResponseXml", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
	public Map<String, String> add(@RequestBody Map<String, String> data) {
		return data;
	}
}

测试

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值