在项目中,遇到一个问题,无论怎么设置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+
    }
}

希望对你有用。

posted on 2016-02-25 17:53  程序诗人  阅读(4037)  评论(0编辑  收藏  举报