springmvc使用put,delete方法传参问题,以及使用@PutMapping注解和@DeleteMapping注解

首先我们要知道@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);
    }
)

这篇内容如果对您有所帮助,记得帮我点赞鼓励下啦,谢谢!~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值