简体   繁体   English

JAX-RS参数转换器-使用它的编译时错误

[英]JAX-RS Param Converter- Compile time error using it

This is my param converter 这是我的参数转换器

import org.springframework.data.domain.Sort;

public class MyParamConverter implements ParamConverter<Sort> {
    @Override
    public Sort fromString(String s){
        return new Sort(new Sort.Order(Sort.Direction.ASC, "ds"));
    }

    @Override
    public String toString(Sort mo){
        return mo.toString();
    }

}

this is my paramconverter provider 这是我的paramconverter提供者

@Provider
public class MyParamConverterProvider implements ParamConverterProvider {

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    if(rawType.equals(Sort.class)){
        return (ParamConverter<T>) new MyParamConverter();
    }
    return null;
}

I am trying to use in my API as 我正在尝试在我的API中使用

@GET
@Path("/")
Response read(@QueryParam("sort") Sort order);

I am expecting the jax to map string that I pass in my url eg &sort="asc" to Sort object. 我期望jax映射我在URL中传递的字符串,例如&sort="asc"到Sort对象。 But I am getting an compile time error that have a registered implementation of paramconverter provider . 但是我收到一个编译时错误,该错误have a registered implementation of paramconverter provider I need to find a way when I pass a query param as &sort="somethung" it gets convert to SORT automatically either by using custom annotation or by using Param Converter . 当我将查询参数传递为&sort="somethung"时,我需要找到一种方法,它可以通过使用自定义注释或使用Param Converter自动转换为SORT

with reference to your comment, try registering your provider like: 参考您的评论,尝试注册您的提供商,例如:

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MyParamConverterProvider.class);

        return classes;
    }
}

or, if you are using Jersey 或者,如果您使用的是Jersey

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        packages("my.package");

        // or without package scanning
        register(MyParamConverterProvider.class);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM