Java tutorial
/* * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.edu.zju.bigdata.controller; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.validation.constraints.NotNull; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.HEAD; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import org.reflections.Reflections; import org.springframework.stereotype.Component; //import com.qmino.miredot.annotations.ReturnType; @Component @Path("/api") public class JerseyController { @GET @Path("/_list") @Produces({ "application/json" }) //@ReturnType("java.lang.List<String>") public Response listAPI() { List<String> apiList = new ArrayList<String>(); Reflections reflections = new Reflections("cn.edu.zju.bigdata.controller"); Set<Class<?>> allClasses = reflections.getTypesAnnotatedWith(Path.class); for (Class<?> controller : allClasses) { String apiPath = controller.getAnnotation(Path.class).value(); for (Method method : controller.getMethods()) { // Filter the API with the @RequestMapping Annotation String apiMethod = method.isAnnotationPresent(GET.class) ? "GET" : method.isAnnotationPresent(POST.class) ? "POST" : method.isAnnotationPresent(PUT.class) ? "PUT" : method.isAnnotationPresent(DELETE.class) ? "DELETE" : method.isAnnotationPresent(HEAD.class) ? "HEAD" : null; if (apiMethod == null) continue; if (method.isAnnotationPresent(Path.class)) apiPath += method.getAnnotation(Path.class).value(); // Filter out /_list itself if (apiPath.equals("/_list")) continue; apiList.add(apiMethod + " " + apiPath); } } return Response.ok().entity(apiList).build(); } @GET @Path("/reverse") public Response reverse(@QueryParam("input") @NotNull String input) { return Response.ok().entity(new StringBuilder(input).reverse().toString()).build(); } @GET @Path("/hello/{name}") public Response greet(@PathParam("name") @NotNull String name) { return Response.ok().entity("Hello, " + name).build(); } }