Over a million developers have joined DZone.

Using ValueEncoders with Resteasy

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

The power of Tapestry’s ValueEncoder can be used with RestEasy by implementing javax.ws.rs.ext.ParamConverterProvider

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.services.ValueEncoderSource;
 
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
 
@Provider
public class ValueEncodersProvider implements ParamConverterProvider {
 
    private final ValueEncoderSource valueEncoderSource;
 
    public ValueEncodersProvider(ValueEncoderSource valueEncoderSource) {
        this.valueEncoderSource = valueEncoderSource;
    }
 
    @Override
    public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
        final ValueEncoder<T> encoder = valueEncoderSource.getValueEncoder(rawType);
        if (encoder == null) {
            return null;
        }
 
        return new ParamConverter<T>() {
            @Override
            public T fromString(String value) {
                return encoder.toValue(value);
            }
 
            @Override
            public String toString(T value) {
                return encoder.toClient(value);
            }
        };
    }
 
}



Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}