2

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

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

Jersey(或其他东西?)是否提供了一种方法来做到这一点,还是我必须导入我想要调用的特定 Resource 类,实例化它等等?提前致谢!

4

2 回答 2

0

是的 jersey 是允许路由配置的 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();
    }
}
于 2013-06-26T17:32:10.913 回答
0

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

在我看来,一切acceptif (s == MatchStatus.MATCH) {可以被拉入自己的方法并暴露给用户。

于 2016-01-27T16:44:29.117 回答