ExtJS Integration with JAX-RS

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();
            // 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();
        }
    }
}

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;

    // ... similar for the other methods
}

This makes cool cleanliness pure and ExtJS agnostic:

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

    B read(Integer id){
        // logic
    }

    // and so on...    
}

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, .

+3
2

, .

, param , :

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

    @PathParam("anotherId")
    private String anotherId;

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

REST ,

@- HttpServletRequest;

@Context protected UriInfo uriInfo;

, . from request.getPathInfo(). , , .

0

Source: https://habr.com/ru/post/1795234/


All Articles