When attempting to transfer a JSON Object from the Frontend to the Backend through Angular JS and Java Spring using the JHipster Framework, I encountered an issue. The initial JSON Object structure is as follows:
{
"anzahl": 0,
"category": 0,
"id": 0,
"parkraumId": 0,
"timestamp": "2016-09-07T12:59:04.290Z",
"wochentag": 0
}
However, in order to store it in the database using the Spring Repository method createCrowdsource(crowdsource)
, I needed to filter out the values wochentag
and anzahl
. This resulted in the revised JSON Object as shown below:
{
"category": 0,
"id": 0,
"parkraumId": 0,
"timestamp": "2016-09-07T12:59:04.290Z"
}
My attempt to achieve this involved specifying the
fields: 'category', 'id', 'parkraumId', 'timestamp'
in the Angular JS controller below. However, when passing this parameter back to the Spring Resource using @RequestParam String fields
, the process did not work as expected.
Angular Controller:
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()
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 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);
}
Crowdsource:
/**
* A 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;
}
// More methods...
}