RESTful 层次结构 类 和 JAX RS

RESTful Hirerarchy with classes with JAXRS

想要使用 JAXRS 和 jersey 作为提供者在 REST 中创建以下层次结构

@POST /origination/customers/
@PUT /origination/customers
@GET /origination/customers/{customerId}

@POST /origination/customers/{customerId}/inventory
@PUT /origination/customers/{customerId}/inventory
@GET /origination/customers/{customerId}/inventory/inventoryId

目前所有服务都写在一个 class OriginationService 中,但为了更好的封装,我想知道这些服务是否可以像客户起源一样在一个名为 CustomerOriginationService 的单独 class 中进行折射和 CustomerInventoryService 中的库存来源(这是一个示例场景,我的问题是类似的)

是否可以通过 JAXRS(Jersey) 注解实现上述功能

当然! Ant 是 assemble 不同 类 中 HTTP 方法集的标准方式。您需要使用 @Path 示例 - @Path("/{parameter}")

以下代码可能对您有用 -

控制器接口 -

package com.teducate.api;

import java.io.UnsupportedEncodingException;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
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;

public interface TekEvents {
    @GET
    @Path("/{parameter}")
    @Produces("application/json")
    Response responseMsg( @PathParam("parameter") String parameter,
            @DefaultValue("Nothing to say") @QueryParam("value") String value) throws UnsupportedEncodingException;
}

实施 -

package com.teducate.api.impl;

import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import com.teducate.api.TekEvents;
import com.teducate.bo.TekEventsBO;
import com.teducate.bo.impl.TekEventBOImpl;

@Path("events")
public class TekEventsController implements TekEvents {

    TekEventsBO tekEventsBO;

    public TekEventsController() {
        tekEventsBO = new TekEventBOImpl();
    }

    public Response responseMsg(String parameter, String value) {
        String output = tekEventsBO.responseMsg(parameter, value);
        return Response.status(200).entity(output).build();
    }
}

子资源定位器是我要找的关键字。

下面这篇文章总结得很好 http://docs.oracle.com/javaee/6/tutorial/doc/gknav.html#gkrhr