Giter VIP home page Giter VIP logo

custom-converter-in-spring-data-rest's Introduction

custom-converter-in-spring-data-rest

Minimal sample to simulate https://stackoverflow.com/questions/48931256/how-to-register-converter-in-spring-data-rest-application

You should see JSON schema response with translated enum values:

{
  "title": "Specification",
  "properties": {
    "status": {
      "title": "Status",
      "readOnly": false,
      "type": "string",
      "enum": [
        "de_Draft",
        "de_Active",
        "de_Closed",
        "de_Expired"
      ]
    }
  },
  "definitions": {},
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema#"
}

Imagine that these enum values are used to populated dropdown in frontend UI client in a grid. e.g. statuses in dropdown

When user selects any value, client makes REST call by following e.g. http://localhost:8080/specifications?status=de_Draft what behind the scenes uses org.springframework.data.querydsl.binding.QuerydslPredicateBuilder and this component uses org.springframework.core.convert.support.GenericConversionService.convert

Without any customization, this REST call fails because: Caused by: java.lang.IllegalArgumentException: No enum constant net.homecredit.mer.web.evaluation.spec.Specification.Status.de_Draft

I essentially need to translate translated value de_Draft back to DRAFT enum value. This can be accomplished with EnumTranslator, so I can add TranslationStringToSpecificationStatusEnumConverter:

@Component
public class TranslationStringToSpecificationStatusEnumConverter implements Converter<String, Specification.Status> {

    private final EnumTranslator enumTranslator;

    @Autowired
    public TranslationStringToSpecificationStatusEnumConverter(EnumTranslator enumTranslator) {
        this.enumTranslator = enumTranslator;
    }

    @Override
    public Specification.Status convert(String source) {
        return enumTranslator.fromText(Specification.Status.class, source);
    }
}

and register it with:

@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {

    private final TranslationStringToSpecificationStatusEnumConverter converter;

    @Autowired
    public RepositoryRestConfig(TranslationStringToSpecificationStatusEnumConverter converter) {
        this.converter = converter;
    }

    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        conversionService.addConverter(converter);
        super.configureConversionService(conversionService);
    }
}

but this fails on startup with circular spring bean dependency reason

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  repositoryRestConfig defined in file [C:\dev\minimal-reproducible-samples\custom-converter-in-spring-data-rest\target\classes\mihalcin\customconverter\RepositoryRestConfig.class]
↑     ↓
|  translationStringToSpecificationStatusEnumConverter defined in file [C:\dev\minimal-reproducible-samples\custom-converter-in-spring-data-rest\target\classes\mihalcin\customconverter\TranslationStringToSpecificationStatusEnumConverter.class]
↑     ↓
|  org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration (field java.util.List org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.configurers)
└─────┘

custom-converter-in-spring-data-rest's People

Contributors

pmihalcin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.