Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些关于REST API Response的问题 #17

Open
ghost opened this issue Apr 13, 2017 · 5 comments
Open

一些关于REST API Response的问题 #17

ghost opened this issue Apr 13, 2017 · 5 comments

Comments

@ghost
Copy link

ghost commented Apr 13, 2017

在实现“返回结果按照healthWarningLevel从高到低进行排列,每页显示两个数据”这个要求时,我在postman中输入了相应的GET请求,得到了返回数据,但是返回的response中的内容非常冗长,有一些内容是作业中不需要显示的,如图所示
1

我想请教大家的是

  1. 如何能不显示(忽略)”latitude”和“longitude”信息;
  2. 为什么在每页显示完两组数据后,我会有这样一堆内容呢?

2

自己也尝试加了一些@JsonIgnore@JsonProperty之类的annotation,但是对这些不是很熟悉,所以一直研究不出来,感谢大家能帮忙看看这些问题,谢谢!

@ghost ghost changed the title 一些关于REST API Rresponse的问题 一些关于REST API Response的问题 Apr 13, 2017
@ghost ghost closed this as completed Apr 13, 2017
@ghost ghost reopened this Apr 13, 2017
@ghost
Copy link
Author

ghost commented Apr 13, 2017

对于问题1: 需要在RunningInformation中对 “latitude”, "longitude"等加getter,然后为其加需要的annotation;
对于问题2:可以使用getContent()方法且在返回时不直接返回Page,而是返回List;
感谢大家的帮助!

@zhuzzz
Copy link

zhuzzz commented Apr 13, 2017

请问在返回json response的时候,
怎样把

"userInfo:{
    "userName" : xxx
    "userAddress": xxx
}" 

flatten成

"userName" : xxx 
"userAddress": xxx

谢谢!

@zhuzzz
Copy link

zhuzzz commented Apr 13, 2017

对于问题1,我是直接在"latitude"前面加了个
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)

http://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization

@ghost
Copy link
Author

ghost commented Apr 13, 2017

对于问题1:我是这样做的,想要忽略哪个property
step1 - 在RunningInformation中为其创建getter (例如“userInfo”,“latitude”, “longitude”)
step2 - 在这些getter前加上如下annotations
@JsonIgnore
@JsonProperty(value = "xxxx")

@hackjutsu
Copy link
Member

hackjutsu commented Apr 15, 2017

我的处理方法是先建立一个JSONObject,然后按照作业要求的格式去设置它。

@RequestMapping(value = "/heartRateGreaterThan/{heartRate}", method = RequestMethod.GET)
public ResponseEntity<?> findByHeartRateGreaterThan(
        @PathVariable Integer heartRate,
        @RequestParam(name = "page", required = false, defaultValue = kDefaultPage) Integer page,
        @RequestParam(name = "size", required = false, defaultValue = kDefaultItemPerPage) Integer size) {

    Page<RunningInformation> rawResults = this.runningInformationService.findByHeartRateGreaterThan(heartRate, new PageRequest(page, size));
    List<RunningInformation> content = rawResults.getContent();

    // Transform RunningInformation to customized JSON objects
    List<JSONObject> results = new ArrayList<JSONObject>();
    for (RunningInformation item : content) {
        JSONObject info = new JSONObject();
        info.put("runningId", item.getRunningId());
        info.put("totalRunningTime", item.getTotalRunningTIme());
        info.put("heartRate", item.getHeartRate());
        info.put("userId", item.getId());
        info.put("userName", item.getUserInfo().getUsername());
        info.put("userAddress", item.getUserInfo().getAddress());
        info.put("healthWarningLevel", item.getHealthWarningLevel());
        results.add(info);
    }

    return new ResponseEntity<List<JSONObject>>(results, HttpStatus.OK);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants