从URL获取对Jersey REST资源方法的引用

时间:2013-06-26 17:24:19

标签: rest jersey

我正在进行一些性能测试,我希望能够在不通过网络的情况下调用资源方法。我已经有了生成网址的框架,我希望能够重复使用它。

例如,给定URL:www.example.com:8080 / resources / method,我想获得它调用的资源方法的引用,这样我就可以在不进行网络级HTTP请求的情况下运行它。即,在下面的示例中,我想使用URL" www.frimastudio.com:8080 / time"获取对getServerTime()方法的引用,然后我可以直接调用它。

Jersey(或其他什么?)是否提供了这样做的方法,或者我是否必须导入我想要调用的特定Resource类,实例化它等等?提前谢谢!

2 个答案:

答案 0 :(得分:0)

是泽西是RESTful API,允许路由配置(仅限注释)

示例:

package com.frimastudio.webservice.controller.route;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.joda.time.DateTime;

import com.frimastudio.webservice.controller.representation.Time;

@Path("/time")
@Produces(MediaType.APPLICATION_JSON)
public class TimeResource
{

    public TimeResource()
    {
    }

    @GET
    public Time getServerDate()
    {
        return new Time(new DateTime());
    }

}

时间是杰克逊代表:

package com.frimastudio.webservice.controller.representation;

import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Time
{
    @NotEmpty
    @JsonProperty
    private String date;

    public Time()
    {
        // Jackson deserialization
    }

    public Time(String date)
    {
        super();
        this.date = date;
    }

    public Time(DateTime date)
    {
        super();
        this.date = date.toString();
    }
}

答案 1 :(得分:0)

基于查看泽西岛代码,这似乎不可行。查找由HttpMethodRule.Matcher执行,HttpMethodRule.accept是一个私有类,仅用于实现accept

在我看来,if (s == MatchStatus.MATCH) {fname>""之前的所有内容都可以被移植到自己的方法中并向用户公开。