0

I want to call another spring boot on spring boot

I read a lot of articles

Spring RestTemplate GET with parameters

Spring RestTemplate

Many more...

Temporary methods that I can currently use

    final String uri = "http://127.0.0.1:8888/key";
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri)
            .queryParam("id", "1234")
            .queryParam("model", "model")
            .queryParam("name", "name")
            .queryParam("description", "description")
            .queryParam("status", 0)
            .queryParam("mode", 1)
            .queryParam("creationDate", "2021/05/24 12:34:56")
            .queryParam("updatedDate", "2021/05/24 12:34:56");

    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> response = restTemplate.exchange(
            builder.toUriString(),
            HttpMethod.PUT,
            entity,
            String.class);

I want to be able to use the class directly instead of slowly entering all the parameters

public class DataDto {
    private String id;
    private String model;
    private String name;
    private String description;
    private int status;
    private int mode;
    private String creationDate;
    private String updatedDate;

...
}

How can I use the entire class as a parameter at once?

I have tried many similar things, but there are no parameters on my server:

    logger.info("getId:" + dataDto.getId());
    final String uri = "http://127.0.0.1:8888/key";
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put(uri, DataDto.class, dataDto);
Ruyut
  • 151
  • 11

1 Answers1

0

Try something like below. Override toString() method for DataDto

    @Override
    public String toString() {
    return String.format("id=%s&" +
            "model=%s&" +
            "name=%s&" +
            "description=%s&" +
            "status=%s&" +
            "mode=%s&" +
            "creationDate=%s&" +
            "updatedDate=%s", id, model, name, description, status, mode, 
             creationDate, updatedDate);
    }

Formulate URL like below,

    final String uri = String.format("%s?%s", "http://127.0.0.1:8888/key", dataDto.toString())