"converter from @pathvariable domainobject to string? (using controllerlinkbuilder.methodon)" Code Answer

1

found a "solution". it requires a lot copy & paste from spring's classes, but at least it works!

basically i had to copy org.springframework.hateoas.mvc.annotatedparametersparameteraccessor and change two lines:

class annotatedparametersparameteraccessor {
    ...
    static class boundmethodparameter {
        // old: (with this one you can't call addconverter())
        // private static final conversionservice conversion_service = new defaultformattingconversionservice();
        // new:
        private static final formattingconversionservice conversion_service = new defaultformattingconversionservice();

        ...

        public boundmethodparameter(methodparameter parameter, object value, annotationattribute attribute) {
            ...
            // add:
            conversion_service.addconverter(new mynewconverter());
    }

    ...
}

this class get's used by controllerlinkbuilderfactory. so i had to copy & paste that, too.

and this one get's used by controllerlinkbuilder. also copy & paste.

my converter just does mydomainobject.getid().tostring():

public class mynewconverter implements converter<company, string> {
    @override
    public string convert(company source) {
        return source.getid().tostring();
    }   
}

now you can use the copy&pasted controllerlinkbuilder inside the controller and it works as expected!

By Jite on May 7 2022

Answers related to “converter from @pathvariable domainobject to string? (using controllerlinkbuilder.methodon)”

Only authorized users can answer the Search term. Please sign in first, or register a free account.