0

I'm making API for GatherTown Map to insert object with spring.

It works fine when using Resttemplate to post request.

I wanted to migrate this to Webclient, so I searched and tried all day, but all failed.

public String withRestTemplate(RequestDto requestDto){

        ResponseDto gatherTownMap = getMap(requestDto).share().block();

        SampleObject gatherTownSampleMusic = new GatherTownSampleMusic();
        gatherTownSampleMusic.setX(10);
        gatherTownSampleMusic.setY(10);
        gatherTownSampleMusic.sound.setSrc("https://cdn.gather.town/storage.googleapis.com/gather-town.appspot.com/internal-dashboard/sounds/jAlXIofmjkQFHBM_oYl-F");
        gatherTownSampleMusic.sound.setLoop(false);
        gatherTownSampleMusic.sound.setVolume(0.5);
        gatherTownSampleMusic.sound.setMaxDistance(30);

        gatherTownMap.getObjects().add(gatherTownSampleMusic);

        requestDto.setMapContent(gatherTownMap);
        requestDto.setSpaceId(requestDto.getSpaceId().replace("/", "\\").replace("%20", " "));

        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<RequestDto> entity = new HttpEntity<>(requestDto, headers);
        return restTemplate.exchange(
                apiUrl,
                HttpMethod.POST,
                entity,
                String.class
        ).getBody();
}

My Failed Webclient

public String withWebclient(RequestDto requestDto){

        ResponseDto gatherTownMap = getMap(requestDto).share().block();

        SampleObject gatherTownSampleMusic = new GatherTownSampleMusic();
        gatherTownSampleMusic.setX(10);
        gatherTownSampleMusic.setY(10);
        gatherTownSampleMusic.sound.setSrc("https://cdn.gather.town/storage.googleapis.com/gather-town.appspot.com/internal-dashboard/sounds/jAlXIofmjkQFHBM_oYl-F");
        gatherTownSampleMusic.sound.setLoop(false);
        gatherTownSampleMusic.sound.setVolume(0.5);
        gatherTownSampleMusic.sound.setMaxDistance(30);

        gatherTownMap.getObjects().add(gatherTownSampleMusic);

        requestDto.setMapContent(gatherTownMap);
        requestDto.setSpaceId(requestDto.getSpaceId().replace("/", "\\").replace("%20", " "));

        WebClient client = WebClient.create(apiUrl);
        return  client.
                post().
                contentType(MediaType.APPLICATION_JSON).
                accept(MediaType.APPLICATION_JSON).
                bodyValue(requestDto).
                retrieve().
                bodyToMono(String.class).block();
}

this is RequestDto Class.

public class RequestDto {

    private String apiKey;
    private String spaceId;
    private String mapId;
    private ResponseDto mapContent;
    
    static class ResponseDto {
    private String mostRecentUpdateId;
    private List<Long> dimensions;
    private String foregroundImagePath;
    private List<Object> spaces;
    private String collisions;
    private Boolean isTemplate;
    private String backgroundImagePath;
    private Long objectSizes;
    private Boolean useDrawnBG;
    private List<Object> assets;
    private String id;
    private Boolean isPublic;
    private List<Object> announcer;
    private List<Object> spawns;
    private String version;
    private List<Object> objects;
    private List<Object> portals;
    }

}

How can i migrate my Resttemplate method to Webclient?

심프로
  • 5
  • 3
  • What kind of error you see? Can you share the stacktrace of fail? – Alex Apr 15 '22 at 08:16
  • org.springframework.core.codec.CodecException: Type definition error: [simple type, class java.lang.Object]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.metaverse.station.back.web.gathertownApi.RequestDto["mapContent"]->com.metaverse.station.back.web.gathertownApi.ResponseDto["objects"]->java.util.ArrayList[208]->com.metaverse.station.back.web.gathertownA – 심프로 Apr 15 '22 at 08:30
  • i tried to post full stacktrace but failed.. – 심프로 Apr 15 '22 at 08:31
  • com.metaverse.station.back.web.gathertownApi.ResponseDto["objects"]->java.util.ArrayList[208]- --> there is an error when conversion to ResponseDto in 209. row – Gurkan İlleez Apr 15 '22 at 08:38
  • Check this question and replies https://stackoverflow.com/questions/43769301/how-to-customize-springwebflux-webclient-json-deserialization – Alex Apr 15 '22 at 08:39

0 Answers0