Spring Hateoas / HAL throw исключение - PullRequest
0 голосов
/ 10 мая 2018

У меня странная проблема, потому что в моем приложении все работает нормально, после выполнения метода GET я всегда получаю исключение:

2018-05-10 12:17:18.608  WARN 16031 --- [nio-8080-exec-1] tion$ResourceSupportHttpMessageConverter : Failed to evaluate Jackson serialization for type [class com.computeralchemist.domain.components.OpinionDto]: java.lang.IllegalStateException: Cannot override _serializer: had a `org.springframework.hateoas.hal.Jackson2HalModule$HalLinkListSerializer`, trying to set to `org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$NestedEntitySerializer`

Мой объект DTO прост:

@Getter
@Setter
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public abstract class ComputerComponent extends ResourceSupport {

    ....
}

И один из дочерних классов:

@Getter
@Setter
@Document(collection = "cpu")
public class Cpu extends ComputerComponent {

    @Id
    private long productId;

    private CpuParameters cpuParameters;

}

И метод контроллера REST:

@GetMapping(value = "/{id}", produces = "application/json; charset:UTF-8")
@ResponseStatus(HttpStatus.OK)
public ComputerComponent getComponent(@PathVariable("component") String component,
                                      @PathVariable("id") long id) {

    ComputerComponent computerComponent = repositoryProvider.findComponent(component, id);

    if (computerComponent == null)
        throw new ComponentNotFoundException(component, id);

    computerComponent.add(linkTo(methodOn(ComponentsController.class)
            .getComponent(component, id)).withSelfRel());

    computerComponent.add(linkTo(methodOn(ComponentsController.class)
            .getListOfComponents(computerComponent.getComponentType().toString()))
            .withRel("collection"));

    return computerComponent;
}

Json вернул (выглядит правильно, но я не хочу нулевые значения):

{
  "componentType" : "cpu",
  "producent" : "AMD",
  "model" : "Ryzen 5 1600",
    "content" : [ ],
    "links" : [ ]
  },
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/components/cpu/1",
    "hreflang" : null,
    "media" : null,
    "title" : null,
    "type" : null,
    "deprecation" : null,
    "content" : [ ],
    "links" : [ ]
  }, {
    "rel" : "collection",
    "href" : "http://localhost:8080/components/cpu",
    "hreflang" : null,
    "media" : null,
    "title" : null,
    "type" : null,
    "deprecation" : null,
    "content" : [ ],
    "links" : [ ]
  } ]
}

PS: Почему в свойствах ссылок есть "hreflang", "media" и т. Д.?

1 Ответ

0 голосов
/ 06 января 2019

Этот ответ здесь не столько потому, что он является ответом на проблему ОП (которую они, кажется, решили), а потому, что именно он вызвал проблему для меня. Так что, возможно, кому-то еще, ищущему это, помогут ...

Похоже, что если у вас есть:

org.springframework.boot: весна-загрузки-стартер-hateoas '

тогда вы должны удалить

org.springframework.boot: весна-загрузка-стартер-данные отдых "

По крайней мере - это то, что решило проблему для меня. Исключение исчезло, равно как и пустые поля, о которых сообщил OP.

...