2011-02-28 72 views
3

我正在尝试将ExtJS与JAX-RS集成。我用POJOMappingFeature设置了Jersey,并且它工作正常。但我想摆脱胶水代码。每类现在看起来是这样的:将ExtJS与JAX-RS集成

@Path("/helloworld") 
public class A { 

    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput createAction(ExtJsRestInput<B> toCreate) { 
     try { 
      final B created = toCreate.getData(); 
      // logic 
      return new ExtJsRestDataOutput<B>(created); 
     } catch (Exception e) { 
      return new ExtJsRestFailure(); 
     } 
    } 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput readCollectionAction() { 
     try { 
      final List<B> readCollection; 
      //logic 
      return new ExtJsRestDataOutput<List<B>>(readCollection); 
     } catch (Exception e) { 
      return new ExtJsRestFailure(); 
     } 
    } 

    @PUT 
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput updateAction(ExtJsRestInput<B> toUpdate) { 
     try { 
      final B udpated = toUpdate.getData(); 
      // logic 
      return new ExtJsRestDataOutput<B>(udpated); 
     } catch (Exception e) { 
      return new ExtJsRestFailure(); 
     } 
    } 

    @GET 
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput readAction(@PathParam("id") Integer id) { 
     try { 
      final T read; 
      // logic 
      return new ExtJsRestDataOutput<B>(read); 
     } catch (Exception e) { 
      return new ExtJsRestFailure(); 
     } 
    } 

    @DELETE 
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput deleteAction(@PathParam("id") Integer id) { 
     try { 
      // logic 
      return new ExtJsRestSuccess(); 
     } catch (Exception e) { 
      return new ExtJsRestFailure(); 
     } 
    } 
} 

我试着用继承来解决这个问题,而且它转向类似的东西:

public abstract class ExtJsRestAction<T> { 

    @GET 
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public final ExtJsRestOutput readAction(@PathParam("id") Integer id) { 
     try { 
      final T read = read(id); 
      return new ExtJsRestDataOutput<T>(read); 
     } catch (Exception e) { 
      LOG.error("blad wywolania read", e); 
      return new ExtJsRestFailure("FAIL"); 
     } 
    } 

    abstract public T read(Integer id) throws Exception; 

    // ... similar for the other methods 
} 

这使得扩展类新的清洁和ExtJS的不可知:

@Path("/helloworld") 
public class BAction extends ExtJsRestAction<B> { 
    B create(B toCreate){ 
     // logic 
    } 

    B read(Integer id){ 
     // logic 
    } 

    // and so on...  
} 

这将是相当不错的,但是当路径看起来像这样有问题:

@Path("/helloworld/{anotherId}") 

没有(简单)访问anotherId的方法。

我不知道应该寻找解决方案。有什么办法可以编写一个拦截器类,而不是这个基类,它会打包/解压缩ExtJS的对象?或者,也许你可以推荐一个更好的解决方案来整合ExtJS和Java。我使用的是Struts 2,当我添加一个自定义的拦截器时,它在JSON上工作得很好,但我希望能够使用JAX-RS API来更好地实现它。

+0

自你的问题已经过了一年。最后选择了什么? – husayt 2012-04-11 15:33:00

+0

我们回到了使用Struts 2。 – piotrek 2012-04-13 08:46:03

回答

2

如果我明白了这个问题,你需要进入路径参数。

你可以这样做,用你的子类像一个一流水平的路径PARAM变量:

@Path("/helloworld/{anotherId}") 
public class SubClass { 

    @PathParam("anotherId") 
    private String anotherId; 

    @GET 
    public String hello() { 
     return anotherId; 
    } 
} 
0

如果你需要阅读额外的参数一些REST方法,那么你可能会添加到您的基类领域

@Context受保护的HttpServletRequest请求;

@Context protected UriInfo uriInfo;

并从路径获得所需值,例如,来自request.getPathInfo()。你需要自己解析路径,这不是特别好的一段代码,但这样你可以使你的基本抽象类非常通用。