2016-09-07 40 views
2

我嘗試PUT從使用JHipster框架通過角JS和Java春天的前端到後端JSON對象。我的JSON對象看起來如下:角JS和春天 - 如何篩選JSON對象

{ 
"anzahl": 0, 
"category": 0, 
"id": 0, 
"parkraumId": 0, 
"timestamp": "2016-09-07T12:59:04.290Z", 
"wochentag": 0 
} 

但它在數據庫中通過Spring庫方法createCrowdsource(crowdsource)我需要過濾掉的值wochentaganzahl得到以下JSON對象保存:

{ 
"category": 0, 
"id": 0, 
"parkraumId": 0, 
"timestamp": "2016-09-07T12:59:04.290Z" 
} 

我嘗試了通過指定以下角度JS控制器fields: 'category', 'id', 'parkraumId', 'timestamp'@RequestParam String fields得到這個參數回到了春天的資源,但不知何故它doesn't工作。

角控制器:

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', 

      } 
     }); 
    } 


})(); 

春天資源:

/** 
* 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); 
} 

衆包:

/** 
* 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; 
    } 

    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 that = (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 + 
      '}'; 
    } 
} 
+0

你能分享'Crowdsource'類的代碼嗎? – Rohit

+0

是的,我添加了它。 –

回答

1

爲了避免與CrowdSource實體@javax.persistence.Transient註釋堅持,你可以註釋它們的值。對於持久性,這些將被忽略。

@Transient 
private Integer anzahl; 

@Transient 
private Integer wochentag