Javascript Angular JS和Spring-如何过滤JSON对象
我尝试使用JHipster框架,通过Angular JS和Java Spring将一个JSON对象从前端放到后端。我的JSON对象如下所示:Javascript Angular JS和Spring-如何过滤JSON对象,javascript,java,angularjs,json,spring,Javascript,Java,Angularjs,Json,Spring,我尝试使用JHipster框架,通过Angular JS和Java Spring将一个JSON对象从前端放到后端。我的JSON对象如下所示: { "anzahl": 0, "category": 0, "id": 0, "parkraumId": 0, "timestamp": "2016-09-07T12:59:04.290Z", "wochentag": 0 } 但是要通过Spring存储库方法createCrowdsource(crowdsource)将其保存在数据库中,我需要过滤掉wo
{
"anzahl": 0,
"category": 0,
"id": 0,
"parkraumId": 0,
"timestamp": "2016-09-07T12:59:04.290Z",
"wochentag": 0
}
但是要通过Spring存储库方法createCrowdsource(crowdsource)
将其保存在数据库中,我需要过滤掉wochentag
和anzahl
的值,以获得以下JSON对象:
{
"category": 0,
"id": 0,
"parkraumId": 0,
"timestamp": "2016-09-07T12:59:04.290Z"
}
我尝试在下面的Angular JS控制器中指定字段:“category”、“id”、“parkraumId”、“timestamp”
,并使用@RequestParam String fields
在Spring资源中获取此参数,但不知怎的,它不起作用
角度控制器:
function save() {
vm.isSaving = true;
if (vm.crowdsource.id !== null) {
//JSON needs these two attributes
console.log('Updating!')
Crowdsource.update(vm.crowdsource, onSaveSuccess, onSaveError, {fields: 'category', 'id', 'parkraumId', 'timestamp'});
} else {
Crowdsource.save(vm.crowdsource, onSaveSuccess, onSaveError);
}
}
save()
角度服务:
(function () {
'use strict';
angular
.module('bahnApp')
.factory('Crowdsource', Crowdsource);
Crowdsource.$inject = ['$resource', 'DateUtils'];
function Crowdsource($resource, DateUtils) {
var resourceUrl = 'api/crowdsources/:siteId';
return $resource(resourceUrl, {}, {
'query': {method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
if (data) {
data = angular.fromJson(data);
data.timestamp = DateUtils.convertDateTimeFromServer(data.timestamp);
}
return data;
}
},
'update': {
method: 'PUT',
}
});
}
})();
Spring资源:
/**
* PUT /crowdsources : Updates an existing crowdsource.
*
* @param crowdsource the crowdsource to update
* @return the ResponseEntity with status 200 (OK) and with body the updated crowdsource,
* or with status 400 (Bad Request) if the crowdsource is not valid,
* or with status 500 (Internal Server Error) if the crowdsource couldnt be updated
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@RequestMapping(value = "/crowdsources",
method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Crowdsource> updateCrowdsource(@RequestParam String fields, @RequestBody Crowdsource crowdsource) throws URISyntaxException {
log.debug("REST request to update Crowdsource : {}", crowdsource);
log.debug(crowdsource.toString());
if (crowdsource.getId() == null) {
return createCrowdsource(crowdsource);
}
Crowdsource result = crowdsourceRepository.save(crowdsource);
crowdsourceSearchRepository.save(result);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert("crowdsource", crowdsource.getId().toString()))
.body(result);
}
为了避免持久化这些值,可以在
众源
实体中使用@javax.persistence.Transient
注释对它们进行注释。为了持久性,这些将被忽略
@Transient
private Integer anzahl;
@Transient
private Integer wochentag
你能分享众源类的代码吗?是的,我已经添加了。
@Transient
private Integer anzahl;
@Transient
private Integer wochentag