tag:blogger.com,1999:blog-7625178889455096573.post5135916824394886025..comments2022-12-18T23:14:05.457-06:00Comments on my kingdom for a smile :-): Jersey & JerseyTest migration from 1.x to 2.5 with Spring, JSP, Tomcat 7 and FreeMarkerRuss Jacksonhttp://www.blogger.com/profile/15521749913146166813noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-7625178889455096573.post-91267193142965662532014-04-21T17:01:16.378-05:002014-04-21T17:01:16.378-05:00I don't use spring. And jersey configured as ...I don't use spring. And jersey configured as a filter in web.xml doesn't seem to work (it doesn't respond to my REST requests). I rename the filter to the servlet based configuration, and it starts working immediately. Any thoughts?Trenton D. Adamshttps://www.blogger.com/profile/06181286642721324142noreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-12894094600902650622014-02-11T04:28:54.083-06:002014-02-11T04:28:54.083-06:00Also an interesting side question came up.
In t...Also an interesting side question came up. <br /><br />In the case of doing a custom injection, how do you distinguish between to instances of the same class. In my example, I register a bind an instance of a Controller to the Controller class. What happens in the case if I want to two to create two different instances of the Controller. How would that work in terms of the register.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-18845209795505757852014-02-11T04:26:36.981-06:002014-02-11T04:26:36.981-06:00Opps forgot the JerseyTest part for completion:
-...Opps forgot the JerseyTest part for completion:<br /><br />---------------- JerseyTest 2.x -------------------<br /> @Override<br /> protected Application configure() {<br /><br /> ResourceConfig rc = new ResourceConfig(MyResource .class);<br /> Controller ctrl = Mockito.mock(Controller .class);<br /> rc.register(new AbstractBinder(){<br /> @Override<br /> protected void configure() {<br /> bind(ctrl).to(Controller .class);<br /> }<br /> });<br /> rc.register(ms_ctrl);<br /> return rc;<br /> }<br /><br />----------------------------------------------------<br /><br />In terms of the servletContext, it wasn't clear to me how that was set in JerseyTest 2.x.<br /><br />Thanks,<br />DerekAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-19792833944106701752014-02-11T04:22:57.522-06:002014-02-11T04:22:57.522-06:00Hi Russ,
Thanks for the response. I've actua...Hi Russ,<br /><br />Thanks for the response. I've actually decided to do a direct @Inject instead of a servlet context. This is the code I used:<br /><br />---------- Resource ----------------<br />@Path("myresource")<br />public class MyResource {<br /><br /> @Inject Controller ctrl;<br /><br /> @GET<br /> @Produces(MediaType.TEXT_PLAIN)<br /> public String getIt() {<br /> System.out.println(ctrl.getStatement());<br /> return "Got it!";<br /> }<br />}<br /><br />--------- Servlet Container -------------<br />public class TempServletContainer extends ServletContainer <br />{<br /> @Override<br /> public void init() throws ServletException <br /> {<br /> // TODO Auto-generated method stub<br /> super.init();<br /> ResourceConfig rc = this.getConfiguration();<br /> ResourceConfig rc_new = new ResourceConfig(rc);<br /> <br /> rc_new.register(new AbstractBinder(){<br /> @Override<br /> protected void configure() {<br /> final Controller ctrl = new Controller ();<br /> bind(ms_ctrl).to(Controller.class);<br /> }<br /> });<br /> <br /> this.reload(rc_new);<br /> }<br />}<br /><br /><br />-------------------------------------------------<br /><br />The interesting part was that there seems to be a bug in the Jersey Code where it locks up the resource during the initialization. It's specified here:<br /><br />http://jersey.576304.n2.nabble.com/Container-reload-ResourceConfig-is-locked-td7580847.html<br /><br />However, I'm still curious to see how to do it with the ServletContext. I've actually seen the link you sent me, but this is all new to me so I couldn't get it to work with the TestContainerFactory/TestContainer.<br /><br />In addition, it's still not clear to me what the difference is between using @Context vs. @Inject and using the ServletContext vs. a ResourceConfig. Do you have an insights?<br /><br />Thanks,<br />Derek<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-83099962079636942832014-02-10T22:54:52.137-06:002014-02-10T22:54:52.137-06:00Hi Derek,
It appears that the Grizzly2 test frame...Hi Derek,<br /><br />It appears that the Grizzly2 test framework provider, which I am using, doesn't support injection of the ServletContext. <br /><br />I found an article on StackOverflow that addresses this and offers a solution. See: http://stackoverflow.com/questions/17973277/problems-running-jerseytest-when-dealing-with-httpservletresponse/17989236#17989236<br /><br />I'll see if I can adapt the example to my use case. I'll need to tinker with the way they're injecting the service provider packages into the TestContainer so it's not hard-coded.<br /><br />Might be a couple days before I can get to it.<br /><br />Russ<br />Russ Jacksonhttps://www.blogger.com/profile/15521749913146166813noreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-15572864626555956122014-02-09T13:51:15.339-06:002014-02-09T13:51:15.339-06:00Hi, in your example how would you set the Servelt ...Hi, in your example how would you set the Servelt Context with the ResourceConfig? This is all new to me. <br /><br />Example:<br /><br />@Path(value = "/service")<br />public class Foo{<br /><br />@Context ServletContext ctx;<br /><br />@GET<br />@Path(value="/list")<br />public String list() {<br /> Controller ctrl = new Controller();<br /> ctx.setAttribute("controller", ctrl);<br /> return ctrl.getList();<br />}<br /><br />}<br /><br />public class FooUnitTest extends JerseyTest<br />{<br /> @Test<br /> public void testService()<br /> {<br /><br /> //set/how to configure the context?<br /> }<br />}<br /><br />Thanks,<br />DerekAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7625178889455096573.post-69881421268574013082014-01-22T01:12:44.129-06:002014-01-22T01:12:44.129-06:00Thanks a lot! After countless hours of searching, ...Thanks a lot! After countless hours of searching, finally found this working solution :)<br /><br />If we are using only annotation based spring config, test cases won't load properly because current version of jersey spring expects xml config. We overcame that with putting an application context only for testing and enabling context config from that: ""nobodyhttps://www.blogger.com/profile/09925985265032500938noreply@blogger.com