Spring Boot @DeleteMapping
使用DELETE方式进行交互
说明:ResponseData为自定义返回体{String code, String msg, List<?> data}
PollutionData 为一个entity 属性部分包含{String id, String name}
CodeEnum、MsgEnum为自定义枚举类,定义了一些常量
两种方式皆测试过
环境:win7+idea2018.2+jdk10.0.2+springboot 前端编辑工具为hbuilder
两种方式:
1、
//方法一 使用POST+ _method:"DELETE" + filter(springboot不需要我们配置)
//这里的传输对象为json对象,后台直接接受
var r=confirm("方法一:确认删除该条数据?");
if(r){
//var data = {_method:"DELETE", id:"456456",name:"征集"};
var data = {_method:"DELETE"};//_method:"DELETE"必须,其他属性看你需求
$.ajax({
url:"http://192.168.2.116:8080/pollution/delete/1786vdsds863",
type:"POST",
data:data,
dataType:"json",
success:function(result){
alert(result.msg);
} });
}
@DeleteMapping("/pollution/delete/{id}")
public ResponseData deletePollutionById(@PathVariable("id")String id, @RequestBody PollutionData data){
System.out.println(id);
System.out.println(data);
return new ResponseData(CodeEnum.SUCCESS.getCode(),MsgEnum.SUCCESS.getMsg(),null);
}
2、
//方法二 使用DELETE请求
//这是的传输对象为json字符串 后台使用@RequestBody注解解析该字符串并将字符串映射到对应实体上
var r=confirm("方法二:确认删除该条数据?");
if(r){
var id = "123133";
var jsonstr = { id: id,
name: "12345"};
console.log(jsonstr);
$.ajax({
url:"http://192.168.2.116:8080/pollution/delete/" + id,
type:"DELETE",
contentType:"application/json",//设置请求参数类型为json字符串
data:JSON.stringify(jsonstr),//将json对象转换成json字符串发送
dataType:"json",
success:function(result){
alert(result.msg);
} });
} /**如果不需要传递参数,可以不写下面的几项
* contentType:"application/json",//设置请求参数类型为json字符串
data:JSON.stringify(jsonstr),//将json对象转换成json字符串发送
dataType:"json",
*/
@DeleteMapping("/pollution/delete/{id}")
public ResponseData deletePollutionById(@PathVariable("id")String id, @RequestBody PollutionData data){
System.out.println(id);
System.out.println(data);
return new ResponseData(CodeEnum.SUCCESS.getCode(),MsgEnum.SUCCESS.getMsg(),null);
}
参考:https://blog.csdn.net/liuyuanjiang109/article/details/78972644
Spring Boot @DeleteMapping的更多相关文章
- Spring Boot(spring mvc升级版)
周末挤出了一些时间,学了点东西,总结了一下,可能还有自己理解不到位的地方,希望大家一起交流学习,好东西要大家一起分享嘛~.时间有点紧张,所以样式没有来及做很好的调整,大家就凑活着看吧. Spring ...
- Spring Boot 注解的使用
Spring Boot 优于Spring mvc ,SSM,SSH 的一个亮点就是他使用了好多的注解. 1. @Autowired 这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这 ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- Spring Boot with Spring-Data-JPA学习案例
0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的"接班人",和微服务紧密联系 ...
- Spring Boot系列(一) Spring Boot准备知识
本文是学习 Spring Boot 的一些准备知识. Spring Web MVC Spring Web MVC 的两个Context 如下图所示, 基于 Servlet 的 Spring Web M ...
- Spring Boot 2.x (十):构建优雅的RESTful接口
RESTful 相信在座的各位对于RESTful都是略有耳闻,那么RESTful到底是什么呢? REST(Representational State Transfer)表述性状态转移是一组架构约束条 ...
- 使用Spring Boot Actuator将指标导出到InfluxDB和Prometheus
使用Spring Boot Actuator将指标导出到InfluxDB和Prometheus Spring Boot Actuator是Spring Boot 2发布后修改最多的项目之一.它经过 ...
- 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用
Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用,Spring Boot 的自动配置是通过 Spring 4.x 的条件注解 @Conditional 来 ...
- 前端页面调用Spring boot接口发生的跨域问题
最近要重构一个基于spring boot的后端API服务,需要再本地测试.在本地测试时,运行在本地的前端页面发送一个ajax请求访问后端API,然后浏览器报错blocked CORS policy. ...
随机推荐
- JQuery------实现点击左右按钮,切换图片功能
如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...
- iOS开发之--如何修改TabBarItem的title的字体和颜色/BarButtonItem的title的字体大小和颜色/添加背景图片,并添加点击方法
在进行项目的过程中,我们往往会遇到各种各样的自定义颜色和字体,下面提供一种修改系统自带的TabBarItem的字体和颜色的方法,希望能帮到大家: [[UITabBarItem appearance] ...
- ArcGIS GP应用-GP模型创建-缓冲区分析
一.参考资料 http://www.cnblogs.com/HPhone/archive/2012/11/05/2755974.html 二.关键步骤 1.缓冲分析数据源可以选择:点.面.线 2.构建 ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 用Dialog 做自定义动画,加播放监听
final Dialog customDialog = new Dialog(this); customDialog.setTitle(R.string.attention); customDialo ...
- 160524、Linux下如何启动、关闭Oracle以及打开关闭监听
1. linux下启动oraclesu - oraclesqlplus /nologconn /as sysdbastartupexitlsnrctl startexit2. linux下关闭orac ...
- ZOJ 3605 Find the Marble(dp)
Find the Marble Time Limit: 2 Seconds Memory Limit: 65536 KB Alice and Bob are playing a game. ...
- Servlet------>jsp自定义标签SimpleTag(jsp2.0以后的方法,1-5已经淘汰了)
自定义标签能做什么: 1.移除java代码 2.控制jsp页面某一部分是否执行 3.控制整个jsp是否执行 3.jsp内容重复输出 4.修改jsp内容输出 位置: TagDemo1.java pack ...
- Spark Streaming Programming Guide
参考,http://spark.incubator.apache.org/docs/latest/streaming-programming-guide.html Overview SparkStre ...
- 用Nginx反向代理Node.js
安装pm2 npm install pm2 -g ln -s /home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 /u ...