最近在call restful webService的时候遇到问题,并没有跳转到我想调用的方法里面去。比如我明明call的是add()方法,结果它跳到了delete()方法里面去。还有就是在同一次session里面,我无论call什么方法,它调用的都是同一个方法(而且我测试下来这个方法是随机的-_-#).
主程序是这样:
try{ ClientRequestFactory crf = new ClientRequestFactory(); Test test = = crf.createProxy(InvokerService.class, url); restfulService.add();//call service方法 } catch(Exception e){ e.printStackTrace(); }
InvokerService里是这样:
@POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void add(); @POST @Consumes() @Produces(MediaType.APPLICATION_JSON) public void delete();
restfulService里面是这样:
@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方法的时候它就不会乱跳了。
第二,每次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;//关掉工厂 }
供参考。
restful webService 方法跳转错误的解决方案
原文:http://my.oschina.net/u/2411391/blog/493815