該解決方案對我的作品(DW 0.7.1):
private Multimap<String, String> getEndpoints(Environment environment)
{
Multimap<String, String> resources = ArrayListMultimap.create();
ResourceConfig jrConfig = environment.jersey().getResourceConfig();
Set<Object> dwSingletons = jrConfig.getSingletons();
for (Object singletons : dwSingletons) {
if (singletons.getClass().isAnnotationPresent(Path.class)) {
AbstractResource resource = IntrospectionModeller.createResource(singletons.getClass());
AbstractResource superResource = IntrospectionModeller.createResource(singletons.getClass().getSuperclass());
String uriPrefix = getStringWithoutStartingSlash(resource.getPath().getValue());
for (AbstractResourceMethod srm :resource.getResourceMethods())
{
String uri = uriPrefix;
resources.put(uri,srm.getHttpMethod());
LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName());
}
for (AbstractSubResourceMethod srm :resource.getSubResourceMethods())
{
//extended resources methods will be added by hand
if(superResource != null){
for (AbstractSubResourceMethod superSrm : superResource.getSubResourceMethods())
{
String srmPath = getStringWithoutStartingSlash(srm.getPath().getValue());
String superSrmPath = getStringWithoutStartingSlash(superSrm.getPath().getValue());
Class<?> srmClass = srm.getDeclaringResource().getResourceClass();
Class<?> superSrmClass = superSrm.getDeclaringResource().getResourceClass();
//add superclass method if methodName is not equal superMethodName
if(srmClass.getSuperclass().equals(superSrmClass) && !srm.getMethod().getName().equals(superSrm.getMethod().getName())){
String uri = uriPrefix + "/" + srmPath + "/" + superSrmPath ;
resources.put(uri,superSrm.getHttpMethod());
LOG.info("Found http method " +superSrm.getHttpMethod() + " for the path " + uri + " returning (class) " + superSrm.getReturnType().getName());
}
}
}
String uri = uriPrefix + "/" + getStringWithoutStartingSlash(srm.getPath().getValue());
resources.put(uri,srm.getHttpMethod());
LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName());
}
}
}
return resources;
}
但@PathParam annoations也平淡,例如如果@Path(「/ {id}」)那麼某事。像'.../{id}'將會被使用!
如果您擴展您的資源並且超類也有路徑註釋,那麼此方法將產生的信息甚至超過默認的DW logEndpoints()方法!
FYI:在
類
import java.util.Set;
import javax.ws.rs.Path;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.model.AbstractResource;
import com.sun.jersey.api.model.AbstractResourceMethod;
import com.sun.jersey.api.model.AbstractSubResourceMethod;
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.dropwizard.setup.Environment;
愛這個..! :) – 2016-04-06 14:07:28