Spring Boot Rest API响应更改中的对象字段顺序

问题描述

我有以下模型课程:

package com.restAPIExmaple;

public class ApiModel {
    
    private String City;
    private String TeamName;
    private String QBName;
    
    
    public ApiModel() {
        
    }

    public ApiModel(String city,String teamName,String qBName) {
        
        City = city;
        TeamName = teamName;
        QBName = qBName;
        
    }

    public String getCity() {
        return City;
    }

    public void setCity(String city) {
        City = city;
    }

    public String getTeamName() {
        return TeamName;
    }

    public void setTeamName(String teamName) {
        TeamName = teamName;
    }

    public String getQBName() {
        return QBName;
    }

    public void setQBName(String qBName) {
        QBName = qBName;
    }

}

这是服务类别:

package com.restAPIExmaple;
import java.util.List;
import org.springframework.stereotype.Service;
import java.util.Arrays;

@Service
public class ApiService {
    
    private List <ApiModel> score = Arrays.asList(
            new ApiModel("Jacksonville","Jaguars","Gardner Minshew"),new ApiModel("Tempa Bay","Buccaneer","Tom Brady"),new ApiModel("San Fran","49rs","Jimmy Garoppolo"),);
    
    public List<ApiModel> getscores()
    {
    return score;
    }
    
    public ApiModel getTeam(String team){
        
        return score.stream().filter(t -> t.getTeamName().equalsIgnoreCase(team)).findFirst().get();
        
        
    }   

}

控制器如下:

package com.restAPIExmaple;

import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
@RequestMapping("/football")
public class ApiController {
    @Autowired
    private ApiService apiService;
    
@GetMapping(value = "/scores",produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})

public List<ApiModel> getscores(){
    return apiService.getscores();
    
}

@GetMapping(value="/{team}",MediaType.APPLICATION_JSON_VALUE})
public ApiModel getTeam(@PathVariable String team){
    
    return apiService.getTeam(team);
    
}   
}

这是xml中的响应:

<List>
    <item>
        <teamName>Jaguars</teamName>
        <city>Jacksonville</city>
        <qbname>Gardner Minshew</qbname>
    </item>
    <item>
        <teamName>Buccaneer</teamName>
        <city>Tempa Bay</city>
        <qbname>Tom Brady</qbname>
    </item>
    <item>
        <teamName>49rs</teamName>
        <city>San Fran</city>
        <qbname>Jimmy Garoppolo</qbname>
    </item>
    </List>

问题:对象属性的顺序在输出中已更改。我无法在响应中按该顺序获得“城市”,“团队名称”,“ QBname”。当我使用Eclipse生成getter和setter时,字段的顺序也与模型类不同。任何想法?谢谢。

解决方法

以简单字母开头的变量名称。就是这样