2013-06-26 142 views
2

我正在做一些性能测试,并且希望能够在不通过网络的情况下调用资源方法。我已经有了一个生成URL的框架,我希望能够重用它。从URL中获取对Jersey REST资源方法的引用

例如,给定网址:www.example.com:8080/resource/method,我想获得对它调用的资源方法的引用,以便我可以运行它而不用创建网络级HTTP请求。即,在下面的例子中,我想使用URL“www.frimastudio.com:8080/time”来获得对getServerTime()方法的引用,然后我可以直接调用它。

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

+0

是的,只是实例化你的资源类并调用方法。你不能更直接,更舒适地调用该方法。 – kfis

回答

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(); 
    } 
} 
+0

我认为我的问题还不够清楚。我已经定义了资源方法和全部。我正在从外部类进行性能测试,并且我有URL(www.frimastudio.com:8080/time,来自您的示例)。我知道Jersey可以使用(通过WebResource)在该URL处发出HTTP请求。我想使用该URL来获取对getServerDate()方法的引用,所以我可以直接调用它,而无需通过网络。 – vikrams

+0

如果你想执行一个负载测试脚本,你应该直接调用你的uri作为客户端会做的。如果你真的想绕过http服务器并调用你的函数,你应该使用另一个通信服务,比如websocket。 – nicou50

0

这似乎并不根据查看泽西岛的代码,可以做到这一点。查找由HttpMethodRule.Matcher执行,这是一个专用类,仅用于执行HttpMethodRule.accept

对我来说似乎一切都在acceptif (s == MatchStatus.MATCH) {可能被拉进自己的方法,并暴露给用户。