Using: Java EE + JAX-RS (Apache Wink) + WAS.
Let say I have Rest API declared by class Hello, path "/hello"
@Path("/hello")
public class Hello{
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response sayHello() {
Map<String, String> myMap = new LinkedHashMap<String, String>();
myMap.put("firstName", "To");
myMap.put("lastName", "Kra");
myMap.put("message", "Hello World!");
Gson gson = new Gson();
String json = gson.toJson(myMap);
return Response.status(200).entity(json).build();
}
}
How can I get that path from Hello.class
without using reflections ? I can see example in javax.ws.rs.core.UriBuilder
method path(Class clazz)
which can get it somehow, could not find the sources of it.
Solution with reflections:
/**
* Gets rest api path from its resource class
* @param apiClazz
* @return String rest api path
*/
public static String getRestApiPath(Class<?> apiClazz){
Annotation[] annotations = apiClazz.getAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof Path){
Path pathAnnotation = (Path) annotation;
return pathAnnotation.value();
}
}
return "";
}