springmvc 支持rest风格 一般我们发送请求 都是get请求或post请求 而rest风格告诉我们
那么 普通的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