最近在call restful webService的时候遇到问题,并无跳转到我想调用的方法里面去。好比我明明call的是add()方法,结果它跳到了delete()方法里面去。还有就是在同一次session里面,我不管call什么方法,它调用的都是同一个方法(并且我测试下来这个方法是随机的-_-#).
java
主程序是这样:web
try{ ClientRequestFactory crf = new ClientRequestFactory(); Test test = = crf.createProxy(InvokerService.class, url); restfulService.add();//call service方法 } catch(Exception e){ e.printStackTrace(); }
InvokerService里是这样:restful
@POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void add(); @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void delete();
restfulService里面是这样:session
@Inject Utils utils; @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void add() { try { utils.add(); } catch (Exception e) { e.printStackTrace(); } } @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void delete() { try { utils.delete(); } catch (Exception e) { e.printStackTrace(); } }
个人解决方案有两个方面。第一,在两个service里面的方法上,都加上path这个annotation:测试
@POST @Consumes() @Produces(MediaType.APPLICATION_JSON) @Path("/add")//增长annotation public void add(); @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) @Path("/delete")//增长annotation public void delete(); @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) @Path("/add")//增长annotation public void add() { try { utils.add(); } catch (Exception e) { e.printStackTrace(); } } @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) @Path("/delete")//增长annotation public void delete() { try { utils.delete(); } catch (Exception e) { e.printStackTrace(); } }
这样在call service方法的时候它就不会乱跳了。url
第二,每次call完都关掉代理:代理
ClientRequestFactory crf = null; Test test = null; try{ crf = new ClientRequestFactory(); test = crf.createProxy(InvokerService.class, url); restfulService.add();//call service方法 } catch(Exception e){ e.printStackTrace(); } finally{ test = null;//关掉代理类 crf = null;//关掉工厂 }
供参考。
rest