Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParamConverterProvider method return type mismatch

In the below code snippet I keep receiving the following error in the Provider class.

Type mismatch: cannot convert from DemoParamConverter to ParamConverter

package com.ofss.shop.application.translators;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.Provider;

@Provider
public class DemoParamConverterProvider {

    private final DemoParamConverter dpc = new DemoParamConverter();

    public <T> ParamConverter<T> getConverter(Class<T> rawType,
            Type genericType, Annotation[] annotations) {

        return dpc;
    }


}

package com.ofss.shop.application.translators;
import javax.ws.rs.ext.ParamConverter;
import com.restfully.shop.domain.JavaConversionTarget;
public class DemoParamConverter implements ParamConverter<JavaConversionTarget> {

    @Override
    public JavaConversionTarget fromString(String value) {

        JavaConversionTarget jct = new JavaConversionTarget();
        jct.setJctName(value);
        return jct;
    }

    @Override
    public String toString(JavaConversionTarget value) {
        return value.toString();
    }

}

public class JavaConversionTarget {

    private String jctName;

    /**
     * @return the jctName
     */
    public String getJctName() {
        return jctName;
    }

    /**
     * @param jctName the jctName to set
     */
    public void setJctName(String jctName) {
        this.jctName = jctName;
    }

    @Override
    public String toString() {      
        return "JavaConversionTarget-toString with jctName"+ jctName;
    }

}

I'm using Eclipse Kepler, JDK1.7, Jersey2.4.

like image 575
VedVrat Avatar asked Jun 17 '14 05:06

VedVrat


1 Answers

I got the same problem and ended up by returning an anonymous ParamConverter:

@Provider
public class MyConverterProvider implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType,
            final Annotation[] annotations) {
        if (rawType.getName().equals(MyBean.class.getName())) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(String value) {
                    MyBean myBean = new MyBean();
                    myBean.setValue(value);
                    return rawType.cast(myBean);
                }

                @Override
                public String toString(T myBean) {
                    if (myBean == null) {
                        return null;
                    }
                    return myBean.toString();
                }
            };
        }
        return null;
    }
}

I pushed an example here if that helps.

like image 81
Xavier Coulon Avatar answered Nov 03 '22 01:11

Xavier Coulon