java - 使用 JERSEY 设置重复的 REST 服务

标签 java jersey

我有一个使用 JERSEY 的有效 REST 设置。我需要为一组不同的实体提供几乎相同的功能。我需要做什么来克隆当前的功能?

@Path("/will")
public class FileResource {

private final BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
private final BlobInfoFactory blobInfoFactory = new BlobInfoFactory();

/* step 1. get a unique url */

@GET
@Path("/url")
public Response getCallbackUrl() {
  /* this is /_ah/upload and it redirects to its given path */
  String url = blobstoreService.createUploadUrl("/rest/will");
  return Response.ok(new FileUrl(url), MediaType.APPLICATION_JSON).build();
}

/* step 2. post a file */

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void post(@Context HttpServletRequest req, @Context HttpServletResponse res) throws IOException, URISyntaxException {
  Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
  BlobKey blobKey = blobs.get("files[]");
  res.sendRedirect("/rest/will/" + blobKey.getKeyString() + "/meta");
}

....

我可以简单地复制这个类并将其更改为其他内容吗?

最佳答案

Jersey 没有什么神奇之处,您可以像往常一样进行父类(super class)。例如:

public class BaseResource
{

  @GET
  @Path("/url")
  public Response getCallbackUrl() {
    // Default code goes here
  }
}

@Path("/will")
public class WillResource extends BaseResource
{
  // Overrides go here
}

@Path("/abc")
public class AbcResource extends BaseResource
{
  // Overrides go here
}

这将为您提供/will/url 和/abc/url 的响应

关于java - 使用 JERSEY 设置重复的 REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898512/

相关文章:

Java邮件: attach html content along with an attachment

java - 向 jar 文件添加签名可防止其被加载

java - 从 GET 请求中获取 Jersey 中的 header

java - 使用 Jersey 添加 header 授权

java - 如何使用 Hibernate/JPA 保存自定义 map ?

java - 为什么在elasticSearch中排序不能正确排序数据?

java - 无法从 Swagger + Jersey 获取 json

java - 当JAVA中不存在REST API方法时返回JSON而不是html错误页面

java - 在 weblogic 中启用 Jersey 日志记录

java - 无法读取自定义部署程序扩展代码中的 Tridion 自定义配置 xml 文件节点值