Потребитель Spring HAL возвращает пустой ресурс - PullRequest
0 голосов
/ 24 мая 2018

Я хочу рекурсивно использовать конечные точки в формате HAL.Пример ответа конечной точки выглядит следующим образом:

{
  "_embedded" : {
    "machineStateClassAnswers" : [ {
      "name" : 1,
      "extendedTimeSegmentAnswer" : {
        "duration" : 24,
        "goodParts" : 87,
        "rejectedParts" : 16,
        "relativeDurationInPercentage" : 17,
        "count" : 7,
        "averageDuration" : 3.5714
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:1890/equipment/class_1?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        },
        "class" : {
          "href" : "http://localhost:1890/equipment/class_1?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        }
      }
    }, {
      "name" : 2,
      "extendedTimeSegmentAnswer" : {
        "duration" : 34,
        "goodParts" : 54,
        "rejectedParts" : 11,
        "relativeDurationInPercentage" : 24,
        "count" : 5,
        "averageDuration" : 7.8
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:1890/equipment/class_2?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        },
        "class" : {
          "href" : "http://localhost:1890/equipment/class_2?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        }
      }
    }, {
      "name" : 3,
      "extendedTimeSegmentAnswer" : {
        "duration" : 86,
        "goodParts" : 76,
        "rejectedParts" : 15,
        "relativeDurationInPercentage" : 60,
        "count" : 7,
        "averageDuration" : 21.5714
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:1890/equipment/class_3?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        },
        "class" : {
          "href" : "http://localhost:1890/equipment/class_3?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:1890/equipment/classes?start=3&end=75&sap_keys=7000_3333&sap_keys=7000_4444"
    }
  }
}

Я пытаюсь использовать эту конечную точку, используя класс restTemplate с Jackson2HalModule из Spring HATEOAS, например:

@Service
public class TableResponseService {

    @Value("${url}")
    String url;

    private final RestTemplate restTemplate;

    public TableResponseService(RestTemplateBuilder restTemplateBuilder) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.registerModule(new Jackson2HalModule());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
        converter.setObjectMapper(mapper);
        this.restTemplate = restTemplateBuilder.additionalMessageConverters(Arrays.asList(converter)).build();
    }

    public Resources<List<MachineStateClassAnswer>> getMachineStates() {
        return restTemplate.exchange(url,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<Resources<List<MachineStateClassAnswer>>>() {
                }
        ).getBody();
    }
}

Классы данных:

public class MachineStateClassAnswer extends ResourceSupport {
    private Long name;
    public ExtendedTimeSegmentAnswer extendedTimeSegmentAnswer;

    public MachineStateClassAnswer(Long name, ExtendedTimeSegmentAnswer extendedTimeSegmentAnswer) {
        this.name = name;
        this.extendedTimeSegmentAnswer = extendedTimeSegmentAnswer;
    }

    public Long getName() {
        return name;
    }

    public ExtendedTimeSegmentAnswer getExtendedTimeSegmentAnswer() {
        return extendedTimeSegmentAnswer;
    }


    public void setName(Long name) {
        this.name = name;
    }

    public void setExtendedTimeSegmentAnswer(ExtendedTimeSegmentAnswer extendedTimeSegmentAnswer) {
        this.extendedTimeSegmentAnswer = extendedTimeSegmentAnswer;
    }
}

А

public class ExtendedTimeSegmentAnswer extends BasicTimeSegmentAnswer {
    protected Long relativeDurationInPercentage;
    protected Long count;
    protected Double averageDuration;

    public ExtendedTimeSegmentAnswer() {
    }

    public ExtendedTimeSegmentAnswer(Long duration, Long goodParts, Long rejectedParts, Long count, Double averageDuration) {
        super(duration, goodParts, rejectedParts);
        this.count = count;
        this.averageDuration = averageDuration;
    }

    public Long getRelativeDurationInPercentage() {
        return relativeDurationInPercentage;
    }

    public Long getDuration() {
        return duration;
    }


    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public Double getAverageDuration() {
        return averageDuration;
    }

    public void setAverageDuration(Double averageDuration) {
        this.averageDuration = averageDuration;
    }

}

А:

public class BasicTimeSegmentAnswer {
    protected Long duration;
    protected Long goodParts;
    protected Long rejectedParts;

    public BasicTimeSegmentAnswer() {
    }

    public BasicTimeSegmentAnswer(Long duration, Long goodParts, Long rejectedParts) {
        this.duration = duration;
        this.goodParts = goodParts;
        this.rejectedParts = rejectedParts;
    }

    public Long getDuration() {
        return duration;
    }

    public void setDuration(Long duration) {
        this.duration = duration;
    }

    public Long getGoodParts() {
        return goodParts;
    }

    public void setGoodParts(Long goodParts) {
        this.goodParts = goodParts;
    }

    public Long getRejectedParts() {
        return rejectedParts;
    }

    public void setRejectedParts(Long rejectedParts) {
        this.rejectedParts = rejectedParts;
    }

}

Мой тест:

@Test
public void testConnection(){
    System.out.println(tableResponseService.getMachineStates());
}

Однако возвращает:

Resources { content: [], links: [] }

Что бы я ни пытался изменить, я просто не могу заставить его работать.

1 Ответ

0 голосов
/ 25 мая 2018

Для тех, у кого есть похожие проблемы, вот решение:

  1. Исправьте конвертер сообщений с this.restTemplate = restTemplateBuilder.messageConverters(converter).build(); (... поскольку конвертеры по умолчанию существуют для *+json)

  2. Добавить конструктор по умолчанию в MachineStateClassAnswer public MachineStateClassAnswer() {}

...