All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.jersey.params.NonEmptyStringParamFeature Maven / Gradle / Ivy

There is a newer version: 5.0.0-rc.1
Show newest version
package io.dropwizard.jersey.params;

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Optional;

/**
 * A class for describing how Jersey should serialize a {@link NonEmptyStringParam}. If the
 * parameter was not detected in the response, instead of the resulting value being null, it will
 * evaluate to {@link Optional#empty()}
 */
public class NonEmptyStringParamFeature implements Feature {
    @Override
    public boolean configure(final FeatureContext context) {
        context.register(new ParamConverterProvider() {
            @Override
            public  ParamConverter getConverter(final Class rawType,
                                                      final Type genericType,
                                                      final Annotation[] annotations) {
                return (rawType != NonEmptyStringParam.class) ? null : new ParamConverter() {
                    @Override
                    public T fromString(final String value) {
                        return rawType.cast(new NonEmptyStringParam(value));
                    }

                    @Override
                    public String toString(final T value) {
                        return value == null ? null : value.toString();
                    }
                };
            }
        });
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy