日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

玩JerseyTest(Jersey 2.5.1和DI)

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 玩JerseyTest(Jersey 2.5.1和DI) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我將嘗試解釋一個簡單的REST示例。 這個想法是建立一個基本的架構來開始使用Jersey。 當我開始使用某些框架時,通常會開發一個快速失敗的測試環境,這就是我要做的。

下一個示例具有以下功能:

  • 澤西島2.5.1
  • 依賴注入
  • 用于測試的JUnit


類:

  • 資源:它將參加HTTP調用。
  • 服務:這是具有兩個實現Impl1和Impl2的接口。
  • ServiceProvider:它將在運行時為每個請求調用提供適當的Service實現。
  • TestBinder:將綁定設置為Resource。
import static org.junit.Assert.assertEquals;import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Application; import javax.ws.rs.core.Response;import org.glassfish.hk2.api.Factory; import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.process.internal.RequestScoped; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.Test;public class JerseyInjectionTest extends JerseyTest {private static final String EXPECTED_CONTENT = "any string :P";/*** Checks that the Resource uses Impl1.class*/@Testpublic void invokeImpl1(){invoke(Impl1.class);}/*** Checks that the Resource uses Impl2.class*/@Testpublic void invokeImpl2(){invoke(Impl2.class);}/*** Checks that Resource.anyContent has always the value of EXPECTED_CONTENT*/@Testpublic void checkContent(){Response response = target("example/content").request().get();assertEquals(EXPECTED_CONTENT, response.readEntity(String.class));}private <T extends Service> void invoke(Class<T> service){final String serviceName = service.getName();Response response = target("example/"+serviceName).request().get();assertEquals(service.getName(), response.readEntity(String.class));}/*** Register the Resource and TestBinder in the Application*/@Overrideprotected Application configure() {return new ResourceConfig() {{register(new TestBinder());register(Resource.class);}};}@Path("/example")public static class Resource {@InjectService service;@InjectString anyContent;/*** Returns the name of the Service's implementation*/@GET@Path("/{serviceClass}")public Response getDynamicInvokedService() {return Response.ok(service.getClass().getName()).build();}/*** Returns always the value of anyContent*/@GET@Path("/content")public Response getStaticContent() {return Response.ok(anyContent).build();}}/*** This class will help Resource to set the @Inject fields.*/public static class TestBinder extends AbstractBinder{@Overrideprotected void configure() {bindFactory(ServiceProvider.class).to(Service.class);bind(EXPECTED_CONTENT).to(String.class);}}/*** This class will instance a Services's implementation* per each time that the Resource is called.*/@RequestScopedpublic static class ServiceProvider implements Factory<Service> {private final String serviceName;public ServiceProvider(@PathParam("serviceClass") String serviceName) {this.serviceName = serviceName;}@Overridepublic void dispose(Service arg0) {}@Overridepublic Service provide() {try {return (Service) Class.forName(serviceName).newInstance();} catch (Exception e) {return null;}}}/*** Dummy services*/public static interface Service {}public static class Impl1 implements Service {}public static class Impl2 implements Service {}}

現在,我們可以輕松嘗試新功能。

希望對您有所幫助。

參考:在TODOdev博客上與我們的JCG合作伙伴 Sergio Molina 一起使用JerseyTest(Jersey 2.5.1和DI) 。

翻譯自: https://www.javacodegeeks.com/2014/01/playing-with-jerseytest-jersey-2-5-1-and-di.html

總結

以上是生活随笔為你收集整理的玩JerseyTest(Jersey 2.5.1和DI)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。