我正在尝试将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来更好地实现它。
自你的问题已经过了一年。最后选择了什么? – husayt 2012-04-11 15:33:00
我们回到了使用Struts 2。 – piotrek 2012-04-13 08:46:03