Jersey RESTful WebService框架学习(六)接收MultivaluedMap类型参数

时间:2023-03-08 20:51:54

现在的web开发中有些工程会觉得实体bean的架构会比较重,现在的持久层的框架的特点也层出不穷,核心思想的ORM在此基础上,提供了很多便捷操作,mybatis,jfinal(内部持久层框架)之类的也诞生了很多工具,包括官方的也不乏很多国产的,说这么多。。。表达一个意思很多框架都提供map形式的入库,说说Jersey 这个RESTful 框架怎么处理前端对象转为map的吧。



一、前端静态页面

<body>
<input type="text" name="name" ng-model="user.name">
<input type="text" name="age" ng-model="user.age">
<input type="button" value="保存" ng-click="save()">
</body>

二、http请求

<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ctrl", function($scope, $http) {
$scope.user = {};
$scope.save = function() {
$http({
method : 'post',
url : "/Jersey/api/1.0/my/MultivaluedMap",
data : "json=" + angular.toJson($scope.user),
headers : {
'Content-Type' : 'application/x-www-form-urlencoded'
}
}).success(function(data) {
alert(data)
});
};
});
</script>

三、后端接收

@SuppressWarnings("unchecked")
@POST
@Path("/MultivaluedMap")
@Consumes("application/x-www-form-urlencoded")
@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8" })
public String bean(MultivaluedMap<String, String> viParams) {
//得到viParams转换为json
System.out.println(viParams.getFirst("json"));
//转为map便于入库
System.out.println((Map<String, String>)JSON.parse(viParams.getFirst("json")));
return viParams.getFirst("json");
}