java jetty json获取请求时间和持续时间

标签 java jax-rs

我正在尝试创建我的第一个非常简单的网络应用程序。我正在使用maven、jetty、restful。我读过一些有关它的主题,但对于完全的初学者来说,它们通常很难禁食。我还没有弄清楚如何尽可能简单地摆脱这两个问题。

  1. 后来我尝试让应用程序变得更加复杂。但我现在想测试一下,如何获取“请求”提交到服务器的时间。我还想了解向客户提供答复所需的时间。我想以 json 格式显示信息。有简单的方法吗?我该怎么做?

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("sample")
    public class HelloWorldService {
    
        @Path("/helloWorld/{first}/{second}/{third}")
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public HelloWorld helloWorld(@PathParam("first") String first, @PathParam("second") String  second, @PathParam("third") String third) {
            return new HelloWorld(first, second, third);
        }
    }
    

还有:

public class HelloWorld {

    private String first;
    private String second;
    private String third;

public HelloWorld(String first, String second, String third) {
    this.first = first;
    this.second = second;
    this.third= third;
}

public HelloWorld(String first, String second) {
    this.first = first;
    this.second = second;
}

public HelloWorld(){
}

public HelloWorld(String first) {
    this.first= first;
}

    public String getFirst() {
        return first;
    }

    public String getSecond(){
        return second;
    }

    public String getThird(){
        return third;
    }
}

谢谢! :)

最佳答案

根据您的需要,您可以简单地测量处理请求所需的时间:

class ResponseWrapper<T> {
    public final Long generationTime;
    public final T response;

    public ResponseWrapper(final Long generationTime, final T response) {
        this.generationTime = generationTime;
        this.response = response;
    }
}

@...
public ResponseWrapper<HelloWorld> helloWorld(...) {
    long startTime = System.currentTimeMillis();

    // process the request

    long elapsedTime = System.currentTimeMillis() - startTime;

    return new ResponseWrapper<HelloWorld>(elapsedTime , new HelloWorld(...));
}

或者,如果您需要一些更通用的解决方案,请使用 filters from Servlet API :

public class GenerationTimeFilter implements Filter {
    ...

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
       long startTime = System.currentTimeMillis();

       chain.doFilter(req, res);

       long elapsedTime = System.currentTimeMillis() - startTime;
    }
}

要了解如何修改过滤器内的 ServletResponse,请参阅此问题:Looking for an example for inserting content into the response using a servlet filter

<小时/>

请记住,这种类型的测量不会为最终用户提供等待获得响应的时间。您的应用程序服务器将需要几毫秒来处理原始 HTTP 请求/响应,更不用说网络延迟了。

关于java jetty json获取请求时间和持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582222/

相关文章:

java - 如何从 Map<int , int[] > 中提取整数数组的长度?

authentication - Wildfly 8 基本身份验证

java - JAX-RS:如何扩展应用程序类来扫描包?

java - RESTful Web 服务生命周期“总是”每个请求?

jquery - AJAX POST/GET 请求抛出空错误,但 Glassfish 服务器似乎响应正确

java - 热 Observables 的 RxJava 延迟

java - 导入后 Keycloak Realm 中缺少用户

spring - TOMEE + Spring + JAX-RS (NoSuchMethodError) jar 冲突

java - 改造错误 JSONSyntaxException

java - 如何实现阻塞ThreadPoolExecutor