首先我们要知道@PutMapping,@DeleteMapping的作用:
@PutMapping:“对应修改操作,表明是一个修改URL映射”。
@DeleteMapping:“对应删除,表明是一个删除URL映射”
使用ajax的put提交方式
$.ajax({
url:"http://127.0.0.1/typeList",
type:"PUT",
data:data,
success:function (result) {
}
});
使用ajax的delete提交方式
var ids = [1,2,3];
$.ajax({
url:"http://127.0.0.1/typeList/"+ids,
type:"DELETE",
success:function (result) {
}
});
如果是使用springmvc需要在web.xml中加入过滤器
<!-- 使用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>
<servlet-name>springmvc</servlet-name>
</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>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
@PutMapping注解和@DeleteMapping的使用
@RestController
@RequestMapping("typeList")
//解决跨域问题
//@CrossOrigin("http://localhost:63342")
public class TypeListController {
@Autowired
private TypeListService typeListService;
@PutMapping
public ResponseEntity<Void> updateTypeList(AoaTypeList typeList){
int count = typeListService.updateTypeList(typeList);
return new ResponseEntity<> ((count>0)?HttpStatus.OK:HttpStatus.BAD_REQUEST);
}
@DeleteMapping("{ids}")
public ResponseEntity<Void> delByTypeId(@PathVariable("ids") Long[] ids){
int count = typeListService.deleteTypeList(ids);
return new ResponseEntity<>((count>0)?HttpStatus.OK:HttpStatus.BAD_REQUEST);
}
)
这篇内容如果对您有所帮助,记得帮我点赞鼓励下啦,谢谢!~