springMVC正确使用GET POST PUT和DELETE方法,如何传递参数


1. 向服务器请求数据:GET

   这是标准的http的GET最擅长的, 应该使用GET请求,但是在使用时候我们会需要传递一个或多个参数给服务器, 

     这些出参数可能是基本数据类型页可能是对象,get方法可以将我们从前台传递的参数直接转换为后台接收的对象,

    但是注意, get最多只能把前台传递的参数解析为一个对象,(既: 跟对象属性一一对应的参数将会被组装成对象),

   不属于的需要单独用@RequestParam接收, 但是也只能接受基本类型的参数,不是接收对象。

举个栗子:

js端:

    $scope.pageChange = function() {
        VehicleApplication.get({
            page: $scope.pageInfos.number ? $scope.pageInfos.number - 1 : 0,  // page和size将会被解析成pageabe对象
            size: $scope.pageInfos.size,
            startTime: $scope.query.startTime, // 其他参数需要以@RequestParam接收
            endTime: $scope.query.endTime,
            status: $scope.query.status}, 
            function(response) {
                $scope.refreshContent(response);
        });
    }

后台接收:

	@GetMapping
	@ResponseBody
	public Page<VehicleApplicationPageDTO> getStartedApplications(
			@RequestParam(required = false) String startTime,
			@RequestParam(required = false) String endTime,
			@RequestParam(required = false, defaultValue = "ALL") String status,
			@PageableDefault(page = 0, size = 10, sort = {"id"}, 
			direction = Sort.Direction.DESC) Pageable pageable){ // 自动组成pageable对象

		...
	}

  

 

2. 提交资源到服务器

  用post

 

3. 更改服务器资源

  用put

 

4. 删除服务器资源

  用delete


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM