spring 应用类对象时ResponseEntity返回空值

smdncfj3  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(391)

我是新的Spring Boot 和对不起的情况下,这是非常基本的,但我张贴,因为我已经尝试了其他方式,并检查类似的线程以及。
如果我使用下面代码,它将返回正确的响应

ResponseEntity<String> responseEntityString = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);

输出量

[{"Id":"123aa","TenId":5198,"Name":"test","Description":"test11","Tags":[]}]

现在我已经创建了如下的工作区类(getter/setter/arg构造函数和no-arg构造函数也在那里)

public class Workspace {

    private String Id;
    private String TenId;
    private String Name;
    private String Description;
    private List<String> Tags;
 }

现在我执行下面的代码-

ResponseEntity<List<Workspace>> response = restTemplate.exchange(
                  url,
                  HttpMethod.GET,
                  requestEntity,
                  new ParameterizedTypeReference<List<Workspace>>(){});
        List<Workspace> employees = response.getBody();
        employees.stream().forEach(entry -> System.out.println(entry.getId() + ": " + entry.getName()));

它回来了

null: null

以下为返回true

System.out.println("Value "+ response.hasBody());

下面是返回的-新值[com.pratik.model.Workspace@3cbf1ba4]

New Values [com.pratik.model.Workspace@3cbf1ba4]

因此,请建议需要更改哪些内容才能获得这些值
初始化的resttemplate bean如下所示

public class app1 {
    static RestTemplate restTemplate = new RestTemplate();
    static String url =   url;
    public static void main(String[] args) {
        SpringApplication.run(app1.class, args);
        getCallSample();
        
    }

===============================================================
更新最新代码

ResponseEntity<Workspace[]> responseNew = restTemplate
                  .exchange(
                    url,
                    HttpMethod.GET,
                    requestEntity,
                    Workspace[].class);
Workspace [] employees1 = responseNew.getBody();
List<Workspace> list = Arrays.asList(employees1);
list.stream().forEach(entry -> System.out.println(entry.getId() + ": " + entry.getName()));

但回应仍然是

null: null

======================================================================================================当尝试使用字符串时另一个更新。类返回

[{"Id":"abc","TenId":11,"Name":"tt1 Workspace","Description":"testtenant Workspace (System Generated)","Tags":[]}]

但是当使用workspace类时-它返回-

[Id=null, TenId=null, Name=null, Description=null, Tags=null, getId()=null, getTenId()=null, getName()=null, getDescription()=null, getTags()=null]

那么使用Workspace[].class是正确的方法吗?

ua4mk5z4

ua4mk5z41#

static RestTemplate restTemplate = new RestTemplate();变量替换为真实的Bean:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class app1 {
    //remove this variable
    //static RestTemplate restTemplate = new RestTemplate();
    static String url = "your_url";
    public static void main(String[] args) {
        SpringApplication.run(app1.class, args);
        //getCallSample();
    }

    //create a proper RestTemplate bean
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //add a converter so you can unmarshall the json content
        MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
        //this is an example to set an ObjectMapper instance
        //you can define a bean to configure the ObjectMapper
        //with specific details like avoid unmarshalling unknown fields
        converter.setObjectMapper(new ObjectMapper());
        restTemplate.getMessageConverters().add(converter);
        return restTemplate;
    }
}

现在,在方法中使用rest模板,从Spring的应用程序上下文中获取,而不是使用您自己的静态bean。

@Component
public class WorkspaceService {

    private final RestTemplate restTemplate;

    public WorkspaceService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public List<Workspace> getWorkspaces() {
        ResponseEntity<List<Workspace>> response = restTemplate.exchange(
              url,
              HttpMethod.GET,
              requestEntity,
              new ParameterizedTypeReference<List<Workspace>>(){});
        List<Workspace> employees = response.getBody();
        employees.stream().forEach(entry -> System.out.println(entry.getId() + ": " + entry.getName()));
        return employees;
    }
}

现在,您可以在组件中使用此Bean。例如,如果要在主类中使用它:

@Configuration
public class app1 {
    static String url = "your_url";
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(app1.class, args);
        WorkspaceService ws = ctx.getBean(WorkspaceService.class);
        ws.getWorkspaces();
    }

    //create a proper bean
    @Bean
    public RestTemplate restTemplate() {
        /* code from above... */
    }
}
z31licg0

z31licg02#

通过将Pojo类(用于获取https://json2csharp.com/code-converters/json-to-pojo类)更改为

public class Root{
    @JsonProperty("Id") 
    public String id;
    @JsonProperty("TenantId") 
    public int tenantId;
    @JsonProperty("Name") 
    public String name;
    @JsonProperty("Description") 
    public String description;
    @JsonProperty("Tags") 
    public ArrayList<Object> tags;
}

并且使用该代码

ResponseEntity<Workspace[]> responseNew = restTemplate
                  .exchange(
                    url,
                    HttpMethod.GET,
                    requestEntity,
                    Workspace[].class);
                Workspace [] employees1 = responseNew.getBody();
list.stream().forEach(entry -> System.out.println(entry.getDescription()+": "+  entry.getId() + ": " + entry.getName()));

谢谢你的回答,我从这些回答中学到了很多东西

相关问题