I am trying to integrate ExtJS with JAX-RS. I install Jersey with POJOMappingFeature and it works great. But I want to get rid of the glue code. Each class now looks like this:
@Path("/helloworld")
public class A {
@POST
@Produces(MediaType.APPLICATION_JSON)
public final ExtJsRestOutput createAction(ExtJsRestInput<B> toCreate) {
try {
final B created = toCreate.getData();
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;
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();
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;
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 {
return new ExtJsRestSuccess();
} catch (Exception e) {
return new ExtJsRestFailure();
}
}
}
I tried to resolve this with inheritance, and he turned to something like this:
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;
}
This makes cool cleanliness pure and ExtJS agnostic:
@Path("/helloworld")
public class BAction extends ExtJsRestAction<B> {
B create(B toCreate){
}
B read(Integer id){
}
}
That would be nice, but there are problems when the path looks like this:
@Path("/helloworld/{anotherId}")
There is no (simple) way to access another.
, . , / ExtJS? , , ExtJS Java. Struts 2, JSON, , , JAX-RS API, .