Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Spring Data Rest ResourceProcessor for Projection Exception

I have created the following Projection

@Projection(name = "select", types = {Organisation.class})
public interface OrganisationSelectProjection {

    Long getId();

    String getName();

}

Which I want to basically use in a "Select" component so I need the least data possible. So I also wanted to remove all the links with a ResourceProcessor, so I created this:

@Bean
public ResourceProcessor<Resource<OrganisationSelectProjection>> organisationProcessor() {
    return resource -> {
        resource.removeLinks();
        return resource;
    };
}

However, it looks like this breaks the entire API since whatever endpoint I hit I get the following exception message org.springframework.hateoas.PagedResources cannot be cast to org.springframework.hateoas.Resource

Any idea what I have doen wrong?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you'd like to keep anonymous class in place, using ResourceSupport instead of Resource can resolve the issue.

@Bean
public ResourceProcessor<ResourceSupport> organisationProcessor() {
    return resource -> {
        resource.removeLinks();
        return resource;
    };
}

But in this case the processor will be used on each and every resource regardless of type of the content (you can check that inside the method body though).

Instead of anonymous descendant of ResourceProcessor you can define it as a stand-alone class:

public class OrganizationResourceProcessor implements ResourceProcessor<Resource<OrganisationSelectProjection>> {
    @Override
    public Resource<OrganisationSelectProjection> process(Resource<OrganisationSelectProjection> resource) {
        resource.removeLinks();
        return resource;
    }
}

@Bean
public OrganizationResourceProcessor  organisationProcessor() {
    return new OrganizationResourceProcessor();
}

The issue you expereinced has something to do with type erasure since there is no any type parameters in the anonymous class implementation. Your definition is type-safe but it lacks that type-related data which is used at runtime when determining whether particular ResourceProcessor can process a resource. So spring-data-rest thinks that anonymous organizationProcessor can process PagedResources and feeds it to the processor where ClassCastException happens.

Not everything spring-data-rest puts through ResourceProcessors is a Resource. There can be different descendants of org.springframework.hateoas.ResourceSupport class (like PagedResources in your case) and majority of them are not inherited from Resource.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda