javascript - Angular JS and Spring - how to filter JSON Object -
i try put
json object frontend backend via angular js , java spring using jhipster framework. json object looks follows:
{ "anzahl": 0, "category": 0, "id": 0, "parkraumid": 0, "timestamp": "2016-09-07t12:59:04.290z", "wochentag": 0 }
but save in database via spring repository method createcrowdsource(crowdsource)
need filter out values wochentag
, anzahl
following json object:
{ "category": 0, "id": 0, "parkraumid": 0, "timestamp": "2016-09-07t12:59:04.290z" }
i tried specifying fields: 'category', 'id', 'parkraumid', 'timestamp'
in following angular js controller , parameter in spring resource @requestparam string fields
somehow doesn´t work.
angular controller:
function save() { vm.issaving = true; if (vm.crowdsource.id !== null) { //json needs these 2 attributes console.log('updating!') crowdsource.update(vm.crowdsource, onsavesuccess, onsaveerror, {fields: 'category', 'id', 'parkraumid', 'timestamp'}); } else { crowdsource.save(vm.crowdsource, onsavesuccess, onsaveerror); } } save()
angular service:
(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 resource:
/** * put /crowdsources : updates existing crowdsource. * * @param crowdsource crowdsource update * @return responseentity status 200 (ok) , body updated crowdsource, * or status 400 (bad request) if crowdsource not valid, * or status 500 (internal server error) if crowdsource couldnt updated * @throws urisyntaxexception if location uri syntax 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 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); }
crowdsource:
/** * crowdsource. */ @entity @table(name = "crowdsource") @cache(usage = cacheconcurrencystrategy.nonstrict_read_write) @document(indexname = "crowdsource") @jsonfilter("myfilter") public class crowdsource implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) private long id; @column(name = "category") private integer category; @column(name = "timestamp") private zoneddatetime timestamp; @column(name = "parkraum_id") private integer parkraumid; private integer anzahl; private integer wochentag; public integer getanzahl() { return anzahl; } public void setanzahl(integer anzahl) { this.anzahl = anzahl; } public integer getwochentag() { return wochentag; } public void setwochentag(integer wochentag) { this.wochentag = wochentag; } public long getid() { return id; } public void setid(long id) { this.id = id; } public integer getcategory() { return category; } public void setcategory(integer category) { this.category = category; } public zoneddatetime gettimestamp() { return timestamp; } public void settimestamp(zoneddatetime timestamp) { this.timestamp = timestamp; } public integer getparkraumid() { return parkraumid; } public void setparkraumid(integer parkraumid) { this.parkraumid = parkraumid; } @override public boolean equals(object o) { if (this == o) return true; if (o == null || getclass() != o.getclass()) return false; crowdsource = (crowdsource) o; if (id != null ? !id.equals(that.id) : that.id != null) return false; if (category != null ? !category.equals(that.category) : that.category != null) return false; if (timestamp != null ? !timestamp.equals(that.timestamp) : that.timestamp != null) return false; return parkraumid != null ? parkraumid.equals(that.parkraumid) : that.parkraumid == null; } @override public int hashcode() { int result = id != null ? id.hashcode() : 0; result = 31 * result + (category != null ? category.hashcode() : 0); result = 31 * result + (timestamp != null ? timestamp.hashcode() : 0); result = 31 * result + (parkraumid != null ? parkraumid.hashcode() : 0); return result; } @override public string tostring() { return "crowdsource{" + "id=" + id + ", category=" + category + ", timestamp=" + timestamp + ", parkraumid=" + parkraumid + '}'; } }
to avoid persisting values can annotate them @javax.persistence.transient
annotation in crowdsource
entity. ignored persistence.
@transient private integer anzahl; @transient private integer wochentag
Comments
Post a Comment