springmvc的PUT请求和DELETE请求

springmvc 支持rest风格 一般我们发送请求 都是get请求或post请求 而rest风格告诉我们

  • 通过GET请求获取资源
  • 通过POST请求 添加资源
  • 通过PUT请求修改资源
  • 通过DELETE请求删除资源

那么 普通的springmvc搭建好之后 我们应该如何支持restful呢
web.xml中配置:

<!--4.使用Rest风格的URI,将页面普通的post请求转为指定的delete或put请求-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

get和post代码就不贴了 因为我们平常玩的就是get和post请求
PUT请求 ajax:

function pauseJob(jobName,jobGroup) {
        $.ajax({
            url:url+"/quartz/pauseJob",
            data:{jobName:jobName,jobGroup:jobGroup},
            dataType:'json',
            type:"PUT",
            success:function (data) {
                if (data.status=='success') {
                    window.location.reload();
                }
            }
        });
    }

后台代码

	@RequestMapping(value = "pauseJob", method = RequestMethod.PUT)
    @ResponseBody
    public String pauseJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(jobName) || StringUtils.isEmpty(jobGroup)) {
            jsonObject.put("status", "error");
        } else {
            quartzService1.pauseJob(jobName, jobGroup);
            jsonObject.put("status", "success");
        }
        return jsonObject.toJSONString();
    }

在发送DELETE请求时 请求参数有多个 在用RequestParma接收的时候接收不到 因此我们用RequestBody接收请求
1.引入jackson-databind包 不引入会报Http415错误

<!--@RequestBody支持-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>

2.DELETE请求

function deleteJob(jobName,jobGroupName,triggerName,triggerGroupName) {
        var jsonstr = {jobName:jobName,jobGroupName:jobGroupName,triggerName:triggerName,triggerGroupName:triggerGroupName};
        $.ajax({
            url:url+"/quartz/deleteJob",
            data:JSON.stringify(jsonstr),
            contentType:"application/json",
            dataType:'json',
            type:"DELETE",
            success:function (data) {
                if (data.status=='success') {
                    window.location.reload();
                }
            }
        });
    }

3.后台:

	@RequestMapping(value = "deleteJob", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteJob(@RequestBody TriggerEntity triggerEntity) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(triggerEntity.getJobName()) || StringUtils.isEmpty(triggerEntity.getJobGroupName())
                || StringUtils.isEmpty(triggerEntity.getTriggerName()) || StringUtils.isEmpty(triggerEntity.getTriggerGroupName())) {
            jsonObject.put("status", "error");
        } else {
            quartzService1.deleteJob(triggerEntity);
            jsonObject.put("status", "success");
        }
        return jsonObject.toJSONString();
    }

这就可以了。
参考博客:
https://blog.csdn.net/tiantiandjava/article/details/46125141#commentBox
https://blog.csdn.net/liuyuanjiang109/article/details/78972644

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值