3

我创建了自己的PackagesResourceConfig,如下所示:

import com.sun.jersey.api.core.PackagesResourceConfig;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ResourceConfigClass extends PackagesResourceConfig {
    @Override
    public Map<String, MediaType> getMediaTypeMappings() {
        Map<String, MediaType> map = new HashMap<String, MediaType>();
        map.put("xml", MediaType.APPLICATION_XML_TYPE);
        map.put("json", MediaType.APPLICATION_JSON_TYPE);
        return map;
    }
}

但是现在当我启动我的应用程序时,它给了我一个错误,上面写着:

包数组不能为 null 或为空

这来自泽西岛的这个源代码:

/**
 * Search for root resource classes declaring the packages as an 
 * array of package names.
 * 
 * @param packages the array package names.
 */
public PackagesResourceConfig(String... packages) {
    if (packages == null || packages.length == 0)
        throw new IllegalArgumentException("Array of packages must not be null or empty");

    init(packages.clone());
}

但是我已经通过设置com.sun.jersey.config.property.packages参数在我的 web.xml 中设置了包,所以它不应该为空。

4

1 回答 1

2

这实际上是一个 Java 问题。与带参数的普通构造函数不同,如果构造函数只有可变参数,则不传入任何内容都是有效的。因此,您不必使用StringorInteger或任何非可变参数时那样重写构造函数。将我的班级更改为此解决了问题:

import com.sun.jersey.api.core.PackagesResourceConfig;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ResourceConfigClass extends PackagesResourceConfig {
    public ResourceConfigClass(String... packages) {    //this constructor needs to be here, do not delete it or else the com.sun.jersey.config.property.packages param can't be passed in.
        super(packages);
    }

    public ResourceConfigClass(Map<String, Object> props) { //this constructor needs to be here, do not delete it or else the com.sun.jersey.config.property.packages param can't be passed in.
        super(props);
    }

    @Override
    public Map<String, MediaType> getMediaTypeMappings() {
        Map<String, MediaType> map = new HashMap<String, MediaType>();
        map.put("xml", MediaType.APPLICATION_XML_TYPE);
        map.put("json", MediaType.APPLICATION_JSON_TYPE);
        return map;
    }
}
于 2013-09-18T22:00:36.417 回答