• JsonPropertyOrder无法为DTO对象进行属性排序


    在项目中,遇到一个问题,无论怎么设置JsonPropertyOrder,都无法正确实现排序功能,问题代码如下:

    @JsonPropertyOrder(value={"courseId","bestGrade","latestGrade","sequences"},alphabetic = true)
    public class UserLearningCourseResponseDto implements UserLearningCourseResponse {
    
        @JsonProperty("courseId")
        private String courseIdentifier;
        @JsonProperty("bestGrade")
        private float bestCourseGrade;
        @JsonProperty("latestGrade")
        private float latestCourseGrade;
        @JsonProperty("sequences")
        private List<UserLearningSequenceResponse> userLearningSequences;
     
         /*--------------------*/
         /*---  Accessors!  ---*/
         /*--------------------*/
    
        @Override
        public JSONObject asJSONObject() throws JSONException
        {
            return (new JacksonJsonMapper()).toJsonObject(this);
        }
    
        @Override
        public String getCourseId()
        {
            return courseIdentifier;
        }
    
        @Override
        public float getBestGrade()
        {
            return bestCourseGrade;
        }
    
        @Override
        public float getLatestGrade()
        {
            return latestCourseGrade;
        }
    
        @Override
        public List<UserLearningSequenceResponse> getSequences() {
            return userLearningSequences;
        }

    其实上面代码有问题的原因就在于属性值和JsonProperty未统一导致的,只需要修改成如下的即可:

    package com.rosettastone.ws.ptsws.client.dto.jackson;
    
    import com.google.common.base.Objects;
    import com.rosettastone.ws.gearz.domain.jackson.JacksonJsonMapper;
    import com.rosettastone.ws.ptsws.dto.UserLearningCourseResponse;
    import com.rosettastone.ws.ptsws.dto.UserLearningSequenceResponse;
    import org.codehaus.jackson.annotate.JsonProperty;
    import org.codehaus.jackson.annotate.JsonPropertyOrder;
    import org.codehaus.jettison.json.JSONException;
    import org.codehaus.jettison.json.JSONObject;
    
    import java.util.List;
    @JsonPropertyOrder(value={"courseId","bestGrade","latestGrade","sequences"})
    public class UserLearningCourseResponseDto implements UserLearningCourseResponse {
    
        private String type = TYPE_FIELD_VALUE;
    
        @JsonProperty("courseId")
        private String courseId;
        @JsonProperty("bestGrade")
        private float bestGrade;
        @JsonProperty("latestGrade")
        private float latestGrade;
        @JsonProperty("sequences")
        private List<UserLearningSequenceResponse> sequences;
    
        /*--------------------*/
        /*---  Accessors!  ---*/
        /*--------------------*/
    
        @Override
        public JSONObject asJSONObject() throws JSONException
        {
            return (new JacksonJsonMapper()).toJsonObject(this);
        }
    
        @Override
        public String getCourseId()
        {
            return courseId;
        }
    
        public void setCourseId(String courseId)
        {
            this.courseId = courseId;
        }
    
        @Override
        public float getBestGrade()
        {
            return bestGrade;
        }
    
        public void setBestGrade(float bestGrade)
        {
            this.bestGrade = bestGrade;
        }
    
        @Override
        public float getLatestGrade()
        {
            return latestGrade;
        }
    
        public void setLatestGrade(float latestGrade)
        {
            this.latestGrade = latestGrade;
        }
    
        @Override
        public List<UserLearningSequenceResponse> getSequences() {
            return sequences;
        }
    
        public void setSequences(List<UserLearningSequenceResponse> sequences) {
            this.sequences = sequences;
        }
    
        /*-----------------------------------------*/
        /*---  Extend the base class contract!  ---*/
        /*-----------------------------------------*/
        @Override
        public String toString()
        {
            //J-
            return Objects.toStringHelper(this).
                    add("courseIdentifier", courseId).
                    add("bestGrade", bestGrade).
                    add("latestGrade", latestGrade).
                    toString();
            //J+
        }
    }

    希望对你有用。

  • 相关阅读:
    若依问题解决(一)
    Java 将两个List转换为流合并List
    后端返回前端文本换行显示,只能在前端再转换
    Java Stream() 流根据对象属性去重
    vue 当前端传回后端,后端使用实体类接收数据显示报错
    js 中 getMonth() 获取的月份比现实少一个月
    vue连个数组对比
    JS链接跳转方法
    ElementUI--表格toggleRowSelection无法选中
    Linux 常用命令
  • 原文地址:https://www.cnblogs.com/scy251147/p/5217867.html
Copyright © 2020-2023  润新知