javascript - Angular JS 和 Spring - 如何过滤 JSON 对象

标签 javascript java angularjs json spring

我尝试使用 JHipster Framework 通过 Angular JS 和 Java Spring PUT 一个 JSON 对象从前端到后端。我的 JSON 对象如下所示:

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

但要通过 Spring Repository 方法 createCrowdsource(crowdsource) 将其保存在数据库中,我需要过滤掉值 wochentaganzahl获取以下 JSON 对象:

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

我通过在以下 Angular JS Controller 中指定 fields: 'category', 'id', 'parkraumId', 'timestamp' 进行尝试,并使用 @RequestParam 字符串字段 但不知何故它不起作用。

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 服务:

(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);
}

众包:

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

最佳答案

为避免保留值,您可以在 CrowdSource 实体中使用 @javax.persistence.Transient 注释对其进行注释。那些将被忽略以保持持久性。

@Transient
private Integer anzahl; 

@Transient
private Integer wochentag

关于javascript - Angular JS 和 Spring - 如何过滤 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39370942/

相关文章:

javascript - 正则表达式执行后返回null?

java - 计算处理差异

java - 如何在 Picasso 库中实现 Pinch Zoom?

angularjs - 与扩展程序外部的 Chrome 书签交互

javascript - 如何在 Javascript 中动态使用子对象值查找父 JSON 对象值?

javascript - Highcharts : Put data labels legend inside columns

javascript - 关闭嵌入在 HTML 中的 PDF 的缩放

Java——从文件中读取。输入流与阅读器

html - 如何在 Ionic List-Item 指令中用 div 替换头像图像?

html - 对自定义属性使用 data- 而不是 x- 前缀有什么好处?