Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

How to handle json list in spring java for jira api

I'm trying to get a list of some issues on a jira board. I'm trying to extend the tutorial project on the spring website. So far I can get the id and key from a single issue since they are normal objects. But I'm failing on getting a list of all current issues.



{ 
  expand:   "schema,names"
  startAt:  0
  maxResults:   50
  total:    250
  issues:   [
        {
         expand:    "operations,versionedRepr…hangelog,renderedFields"
         id:    "36384"
         key:   "PE-1327"
         fields:    {…}
       },
        {
         expand:    "operations,versionedRepr…hangelog,renderedFields"
         id:    "32853"
         key:   "PE-775"
         fields:    {…}
       },
        {
         expand:    "operations,versionedRepr…hangelog,renderedFields"
         id:    "32855"
         key:   "PE-777"
         fields:    {…}
        }
   ]
}

Snippet of my main class:

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.basicAuthentication(auth,auth2).build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            ValueList response = restTemplate.getForObject(
                    "https://jiraURL/rest/agile/1.0/board/67/issue",
                    ValueList.class);
            List<Value> valueData = response.getValueList();

            log.info(valueData.toString());
        };
    }

My Value Pojo for the data inside "issues"

@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {

    private Long id;
    private String key;

    public Value() {
    }

    public Long getId() {
        return this.id;
    }

    public String getKey() {
        return this.key;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setKey(String quote) {
        this.key = quote;
    }

    @Override
    public String toString() {
        return "issues{" +
                "id=" + id +
                ", key='" + key + ''' +
                '}';
    }
}

My ValueList class

public class ValueList {


    private List<Value> valueList;
    //getter and setter

The output I get is just

[]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution!

I forgot to add the Json annotation to the ValueList Pojo class. My ValueList pojo looks like this now:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ValueList {

    @JsonProperty("issues") 
    private List<Value> valueList;

    public ValueList(List<Value> valueList) {
        this.valueList = valueList;
    }

    public List<Value> getValueList() {
        return valueList;
    }

    public void setValueList(List<Value> valueList) {
        this.valueList = valueList;
    }

    public ValueList(){
        valueList = new ArrayList<>();
    }
}

This is my output now: [issues{id=36384, key='PE-1327'}, issues{id=32853, key='PE-775'}, issues{id=32855, key='PE-777'}, issues{id=37475, key='PE-1568'}, issues{id=37277, key='PE-1528'},...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...