简体   繁体   English

来自弹簧引导休息控制器的JSON响应重复

[英]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.

相关问题 修改 Spring Boot Rest Controller 的默认 JSON 错误响应 - Modify default JSON error response from Spring Boot Rest Controller 从 spring-boot 返回 JSON 响应作为 CSV 文件 controller - Return a JSON response as CSV file from spring-boot controller 带有Spring Boot REST控制器和JSON的空字段 - null fields with Spring Boot REST controller and JSON Spring Boot-奇怪的JSON控制器响应 - Spring boot - Strange JSON Controller response 在JSON响应(Spring Boot,REST)中获取映射表的其他字段的空值 - Getting null values for the other fields of a mapped table in JSON response (Spring Boot, REST) 我如何从 spring 启动 rest controller(来自 org.json) - How do I receive a JSONObject from post on spring boot rest controller (from org.json) 如何在 Spring Boot 中使用 RestTemplate 从来自外部 REST API 的 JSON 响应中提取数据? - How to extract data from JSON response coming from external REST API using RestTemplate in Spring Boot? 如何在Spring Boot REST服务中保存和显示来自服务器的json / octet-stream响应? - How to save and display a json/octet-stream response from a server in spring boot REST service? 在 Spring Boot Rest Controller 中处理压缩的 json 请求 - Process compressed json request in Spring Boot Rest Controller 将 MultiPartFile 作为 JSON 请求属性发送到 Spring Boot REST 控制器 - Sending MultiPartFile as JSON request Property to Spring Boot REST COntroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM