3

I created my own PackagesResourceConfig that looks like this:

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;
    }
}

But now when I start my app, it gives me an error that says:

Array of packages must not be null or empty

That comes from this source code in Jersey:

/**
 * 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());
}

But I've already set the packages in my web.xml by setting the com.sun.jersey.config.property.packages param so it shouldn't be null.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

2

This is actually a Java issue. Unlike normal constructors with parameters, if the constructor only has varargs, it's valid to pass in nothing. As a result, you don't have to override the constructor like you would if it took a String or Integer or any non-vararg parameter. Changing my class to this fixed the problem:

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;
    }
}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356