[英]JSON response from spring boot rest controller getting repeated
I was trying to build a rest api using Spring boot 1.5.9.RELEASE and been stuck on this issue. 我试图使用Spring boot 1.5.9.RELEASE构建一个rest api并且一直坚持这个问题。 The post request to api end points works just fine but when comes to get requests the result gets repeated .
对api端点的post请求工作正常,但是当获得请求时,结果会重复 。 The response which the app produces for get request is
应用程序为获取请求生成的响应是
{"data":["Administrator"]}{"data":["Administrator"]}
The associated request mapping class code 关联的请求映射类代码
@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);
}
The Response class 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;
}
}
Any ideas about how to solve the issue? 有关如何解决问题的任何想法? Thanks in advance
提前致谢
You are creating response twice, use below 您正在创建响应两次,请在下方使用
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.