rest - 使用 Jax-RS,如何使 @Path 修饰的根资源类为单例的?

查看:113
本文介绍了rest - 使用 Jax-RS,如何使 @Path 修饰的根资源类为单例的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

@Path("helloworld")
public class HelloWorldResource {
    
    public HelloWorldResource() {
        System.out.println("New HelloWorldResource...");
    }
    
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces("text/plain")
    public String sayHello() {
         // Return the textual content
        return "Hello World";
    }
    
}

每次请求时,HelloWorldResource 都会 new 一个实例,如何将资源类设置为单例的?

解决方案

使用 @Singleton(com.sun.jersey.spi.resource.Singleton) 修饰资源类,可以使资源类为单例的。

这篇关于rest - 使用 Jax-RS,如何使 @Path 修饰的根资源类为单例的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆