Resourcen-Liste in JAX-RS (Jersey)

Hallo alle zusammen,

ich habe eine Frage bzgl. der Resourcen in JAX-RS:

Nehmen wir an ich habe folgende Ressourcen (festgelegt durch @Path-Annotationen):

/hello
/hello/world
/hello/a
/hello/a/b
/hello/a/b/c
/info

Durch die application.wadl bekomme ich ja eine gute Übersicht über alle Resourcen.
Bekomme ich dies auch in meiner Applikation hin?

Konkret ist dieFrage ob ich es schaffe im Handler von /info eineListe aller Resourcen bekommen kann oder ob ich zumindest in /hello alle in der URL tiefer gelagerte Resourcen finden kann?

Hat jemand eine Idee?
Es gibt schon einige Tricks. Zum Beispiel kann ich mir das Resource-Package geben lassen und dann selbst nach den Annotationen suchen aber das muss doch auch einfacher durch das Framework gehen oder? Ich habe aber bisher noch nichts finden können.

Ich würde mich über ein paar Tipps sehr freuen.

Meinereiner
 
Hallo,

ich hätte da im Moment auch nur eine Reflection basierte Variante parat... jedoch bekomme ich "alle" Resources unabhängig vom Package - ich denke das sollte eigentlich reichen.

Mit der Klasse MetaDataResource bekomme man eine Liste aller in der Web-App verfügbaren Jax-RS Resource Path URIs, diese Liste wird über die Klasse MetaDataResponse verpackt an die Clients geschickt.

Basierend auf dem JAX-RS Beispiel mit Jersey unter:
http://www.tutorials.de/enterprise-java-jee-j2ee-spring-co/387359-jax-rs-json.html

Unsere MetaDataResource
Java:
package de.tutorials.app.meta.remote;

import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

@Path("metadata")
public class MetaDataResource {

	@Context
	private Application application;
	
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Path("/resourcePaths")
	public MetaDataResponse getAvailableResourcePaths() {
		return new MetaDataResponse(extractJaxRsResourcePathsAsUris());
	}

	private Collection<URI> extractJaxRsResourcePathsAsUris() {
		Collection<URI> pathUris = new ArrayList<URI>();
		for (Class<?> candidate : application.getClasses()) {
			Path path = candidate.getAnnotation(Path.class);
			if (path != null) {
				for (Method candidateMethod : candidate.getMethods()) {
					Path methodPath = candidateMethod.getAnnotation(Path.class);
					if (methodPath != null) {
						UriBuilder uriBuilder = UriBuilder.fromResource(candidate);
						pathUris.add(uriBuilder.path(candidateMethod).build());
					}
				}
			}
		}
		return pathUris;
	}

}

Die Klasse MetaDataResponse
Java:
package de.tutorials.app.meta.remote;

import java.net.URI;
import java.util.Collection;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MetaDataResponse {

	@XmlElementWrapper(name = "availableResourcePaths")
	@XmlElement(name = "resourcePath")
	private final URI[] availableResourcePaths;

	public MetaDataResponse() {
		this(null);
	}

	public MetaDataResponse(Collection<URI> paths) {
		this.availableResourcePaths = paths.toArray(new URI[paths.size()]);
	}
}

Ausgabe XML:
via: http://localhost:8080/de.tutorials.web.training/resources/metadata/resourcePaths
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metaDataResponse>
	<availableResourcePaths>
		<resourcePath>app/user/%7Bid%7D</resourcePath>
		<resourcePath>metadata/resourcePaths</resourcePath>
	</availableResourcePaths>
</metaDataResponse>

Ausgabe JSON:
via: curl -i -H "Accept: application/json" http://localhost:8080/de.tutorials.web.training/resources/metadata/resourcePaths

Code:
$ curl -i -H "Accept: application/json" http://localhost:8080/de.tutorials.web.training/resources/metadata/resourcePaths
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 25 May 2012 19:29:19 GMT

{"availableResourcePaths":["app/user/%7Bid%7D","metadata/resourcePaths"]}

Javascript:
{"availableResourcePaths":["app/user/%7Bid%7D","metadata/resourcePaths"]}

Gruß Tom
 
Zuletzt bearbeitet von einem Moderator:
Zurück