Spring Boot REST API响应中对象字段的顺序发生变化。

huangapple go评论92阅读模式
英文:

Order of object fields in spring boot rest api response changes

问题

以下是翻译好的代码部分:

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}", produces = {MediaType.APPLICATION_XML_VALUE,
		  						   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>

问题:对象属性的顺序在输出中已更改。我无法在响应中按照 City、Team Name、QB Name 的顺序获取它们。当我使用 Eclipse 生成 getter 和 setter 时,字段的顺序与模型类中的顺序也不同。有什么想法吗?谢谢。

英文:

I have following model class:

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

}

Here is the service class:

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

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

}

The Controller is as below:

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(&quot;/football&quot;)
public class ApiController {
	@Autowired
	private ApiService apiService;
	
@GetMapping(value = &quot;/scores&quot;, produces = {MediaType.APPLICATION_XML_VALUE,
										  MediaType.APPLICATION_JSON_VALUE})

public List&lt;ApiModel&gt; getScores(){
	return apiService.getScores();
	
}

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

Here is the response in xml:

&lt;List&gt;
    &lt;item&gt;
        &lt;teamName&gt;Jaguars&lt;/teamName&gt;
        &lt;city&gt;Jacksonville&lt;/city&gt;
        &lt;qbname&gt;Gardner Minshew&lt;/qbname&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;teamName&gt;Buccaneer&lt;/teamName&gt;
        &lt;city&gt;Tempa Bay&lt;/city&gt;
        &lt;qbname&gt;Tom Brady&lt;/qbname&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;teamName&gt;49rs&lt;/teamName&gt;
        &lt;city&gt;San Fran&lt;/city&gt;
        &lt;qbname&gt;Jimmy Garoppolo&lt;/qbname&gt;
    &lt;/item&gt;
    &lt;/List&gt;

Problem: The order of the object properties has been changed in the out put. I cant get City, Team name , QBname in that order order in the response. When I generate getter and setter using Eclipse, the order of the fields there is different from the model class as well. Any idea? Thank you.

答案1

得分: 2

开始变量名以简单字母开头。这是情况。

英文:

start variable name with simple letters. that's the case

huangapple
  • 本文由 发表于 2020年9月25日 13:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64058392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定