![](/img/trans.png)
[英]Modify default JSON error response from Spring Boot Rest Controller
[英]JSON response from spring boot rest controller getting repeated
我試圖使用Spring boot 1.5.9.RELEASE構建一個rest api並且一直堅持這個問題。 對api端點的post請求工作正常,但是當獲得請求時,結果會重復 。 應用程序為獲取請求生成的響應是
{"data":["Administrator"]}{"data":["Administrator"]}
關聯的請求映射類代碼
@RequestMapping("/get")
public ResponseEntity getAllRoles()throws Exception{
List<Roles> roles = rolesService.getRoles();
Set<String> roleNames = new HashSet<>();
for(Roles r : roles)
roleNames.add(r.getRoleName());
return new ResponseEntity(new Response(roleNames), HttpStatus.OK);
}
Response類
public class Response<T> {
private T data;
public Response() {}
public Response(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
有關如何解決問題的任何想法? 提前致謝
您正在創建響應兩次,請在下方使用
RequestMapping("/get")
public ResponseEntity<?> getAllRoles()throws Exception{
List<Roles> roles = rolesService.getRoles();
Set<String> roleNames = new HashSet<>();
for(Roles r : roles)
roleNames.add(r.getRoleName());
return new ResponseEntity<Object>(roleNames, HttpStatus.OK);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.