这是一个简单的示例,如何在 WAS8 中开发和部署 REST 服务。该示例基于 API 来管理 JSON 配置,即列出、获取、上传等。
package se.xyz;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
//Use the ApplicationPath annotation do specify the URI path to your REST API
@ApplicationPath("api")
public class RestAPIRegistrationServlet extends Application {
@Override
public Set<Object> getSingletons() {
Set<Object> tSet = new HashSet<Object>();
//add an instance of your API class to the set of API´s.
tSet.add(new ConfigRestAPI());
return tSet;
}
}
这是管理 JSON 配置的 API。请注意,API 使用 ConfigManager 来处理配置。示例中不包含此类。
package se.xyz;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.apache.wink.common.model.wadl.WADLGenerator;
//Specify the ConfigRestAPI part of the URI path
@Path("/configs")
public class ConfigRestAPI{
private static Logger _LOGGER = Logger.getLogger(ConfigRestAPI.class.getName());
@javax.ws.rs.OPTIONS
@Produces("application/vnd.sun.wadl+xml")
public org.apache.wink.common.model.wadl.Application getOptions(@Context Application app,
@Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
Set<Object> tObjectSet = app.getSingletons();
Set<Class<?>> tClassSet = new HashSet<Class<?>>();
for (Object tObject : tObjectSet) {
tClassSet.add(tObject.getClass());
}
org.apache.wink.common.model.wadl.Application wadlAppDoc = new WADLGenerator().generate("", tClassSet);
/* modify the wadlAppDoc JAXB model if you want to add additional information */
return wadlAppDoc;
}
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public Response list(@Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
try {
ResponseBuilder tResponseBuilder =
Response.ok(ConfigManager.getInstance().fetchConfigs(), MediaType.APPLICATION_JSON);
Response tResponse = tResponseBuilder.build();
return tResponse;
} catch (Throwable e) {
_LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new WebApplicationException(e, Response.serverError()
.entity(e.getClass().getName() + ", Message: " + e.getMessage()).build());
}
}
private boolean isNullOrBlank(String aVal){
return aVal == null || "".equals(aVal.trim());
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response fetch(@QueryParam("id") String id, @Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
try {
if (isNullOrBlank(id)) {
return Response.serverError().entity("id cannot be null or blank").build();
}
ResponseBuilder tResponseBuilder =
Response.ok(ConfigManager.getInstance().fetchConfig(id), MediaType.APPLICATION_JSON);
Response tResponse = tResponseBuilder.build();
return tResponse;
} catch (Throwable e) {
_LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new WebApplicationException(e, Response.serverError()
.entity(e.getClass().getName() + ", Message: " + e.getMessage()).build());
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response store(String aConfig, @QueryParam("id") String id, @Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
try {
if (isNullOrBlank(id)) {
return Response.serverError().entity("id cannot be null or blank").build();
}
ConfigManager.getInstance().storeConfig(aConfig, id);
} catch (Throwable e) {
_LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new WebApplicationException(e, Response.serverError()
.entity(e.getClass().getName() + ", Message: " + e.getMessage()).build());
}
ResponseBuilder tResponseBuilder = Response.ok();
Response tResponse = tResponseBuilder.build();
return tResponse;
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response update(String aConfig, @QueryParam("id") String id, @Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
try {
if (isNullOrBlank(id)) {
return Response.serverError().entity("id cannot be null or blank").build();
}
ConfigManager.getInstance().updateConfig(aConfig, id);
} catch (Throwable e) {
_LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new WebApplicationException(e, Response.serverError()
.entity(e.getClass().getName() + ", Message: " + e.getMessage()).build());
}
ResponseBuilder tResponseBuilder = Response.ok();
Response tResponse = tResponseBuilder.build();
return tResponse;
}
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public Response delete(@QueryParam("id") String id, @Context HttpServletRequest aHttpServletRequest) {
logAccess(aHttpServletRequest);
try {
if (isNullOrBlank(id)) {
return Response.serverError().entity("id cannot be null or blank").build();
}
ConfigManager.getInstance().deleteConfig(id);
} catch (Throwable e) {
_LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new WebApplicationException(e, Response.serverError()
.entity(e.getClass().getName() + ", Message: " + e.getMessage()).build());
}
ResponseBuilder tResponseBuilder = Response.ok();
Response tResponse = tResponseBuilder.build();
return tResponse;
}
private static void logAccess(HttpServletRequest aHttpServletRequest) {
if (_LOGGER.isLoggable(Level.FINE))
_LOGGER.fine(aHttpServletRequest.getRequestURI() + " called from: " + aHttpServletRequest.getRemoteHost()
+ " (" + aHttpServletRequest.getRemoteAddr() + ")");
}
}
部署 REST API 后,您可以使用以下方法调用它: URI /ContextPath/api/configs/list 上的 Http GET 用于列表操作。
进一步阅读:
http ://www.ibm.com/developerworks/websphere/techjournal/1305_gunderson/1305_gunderson.html