I receive Dto class as get with restTemplate.exchange, but the value doesn't come in

Asked 2 years ago, Updated 2 years ago, 58 views

I'm tired of looking for it, so I upload it.

This is Spring (Boot) Rest Client and calls the URL to get json data.

Get the value as String

If you put the value in Dto, it's empty.

I succeeded by creating a dto list class to bring the list, but I can't put it in dto alone.

http://appsdeveloperblog.com/spring-resttemplate-tutorial/ I made it referring to it

If you know what the problem is, please answer.

Maybe Lombok is the problem. Should I give you a json annotation?

Dto.java

@Data
public class Dto {
    private String str1;
    private int int2;
...

I have to put the id in the header, so I'm going to put it in the entity and write it in the restTemplate.exchange.

Application.java

@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("id", "myid");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        RestTemplate restTemplate = new RestTemplate();

        ResponseEntity<Dto> response = restTemplate.exchange(
                URL, 
                HttpMethod.GET, 
                entity, 
                Dto.class);

        System.out.println(response.getBody());
}

16:08:03.611 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET URL 16:08:03.648 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/json, application/*+json] 16:08:03.746 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK 16:08:03.750 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [com.example.demo.Dto]

Dto(str1=null, int2=0, ...

Application.java

        ResponseEntity<String> response = restTemplate.exchange(
                URL, 
                HttpMethod.GET, 
                entity, 
                String.class);

16:08:03.481 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET URL 16:08:03.487 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/+json, */] 16:08:03.605 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK 16:08:03.606 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json"

{"Dto":{"str1":"string","int2":100,...

{
    "Dto": {
        "str1": "string",
        "int2": 100,
        ...
    },
    "statusCode": 200,
    "statusDesc": "OK"
}

spring spring-boot rest

2022-09-21 19:03

1 Answers

Ask and answer... Since the json data to be received is contained in Dto (code 4), Dto created a variable Dto2. It seems that the getlist worked because it was coincidence and right

Dto2.java
public class Dto2 {
    private Dto dto;
 }

ResponseEntity<Dto2> response = restTemplate.exchange(
                URL, 
                HttpMethod.GET, 
                entity, 
                Dto2.class);


2022-09-21 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.