无法执行:javax.ws.rs.NotSupportedException:无法找到类型的消息正文阅读器:内容类型为T:application / xml

时间:2015-01-08 11:12:10

标签: java xml rest jax-rs resteasy

您好,我在向JAX-RS服务尝试POST xml消息时收到了上述警告。我的代码如下:

实体:

@Entity(name = "rbac_group")
@NamedQueries({
    @NamedQuery(name = "Group.findById", query = "SELECT g FROM rbac_group g WHERE g.id = :id")
})
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Group implements Serializable {

    private static final long serialVersionUID = 1L;    

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "ID")
    @XmlAttribute(name = "id")
    private Long id;

    @Column(name = "GROUP_NAME", unique = true, nullable = false)
    @XmlElement(name = "Name", required = true)
    private String groupName;

    @Column(name = "DESCRIPTION")
    @XmlElement(name = "Description")
    private String groupDescription;

    public Group() {
    }

    // Getters and setters omitted    

}

控制器:

@Stateless
public class GroupController extends AbstractController<Group> {

    public GroupController() {
        super(Group.class);
    }

}

扩展了抽象控制器:

public abstract class AbstractController<T> {

    @PersistenceContext
    EntityManager em;

    private final Class<T> entityClass;

    public AbstractController(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public T create(T entity) throws EJBAccessException, EntityExistsException, Exception {
        em.persist(entity);
        return entity;
    }
}

REST资源:

@Path("group")
public class GroupResource extends AbstractResource {

    @Inject
    GroupController controller;

    public GroupResource() {
        super(Group.class);
    }

    @Override
    protected AbstractController getController() {
        return controller;
    }

}

扩展了抽象资源:

public abstract class AbstractResource<T> {

    private final Class<T> entityClass;

    public AbstractResource(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract AbstractController getController();

    public Type getType() {
        Type type = ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
        return new AbstractList(type);
    }

    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response post(T entity) {
        try {
            T returnedEntity = (T) getController()
                    .create(new GenericEntity(entity, getType()));
            return Response.status(Response.Status.CREATED)
                    .entity((T) new GenericEntity(returnedEntity, getType())).build();
        } catch (EJBAccessException ex) {
            Logger.getLogger(entityClass.getName())
                    .log(Level.INFO, ex.getMessage());
            return Response.status(Response.Status.FORBIDDEN)
                    .entity(ex.getMessage()).build();
        } catch (EntityExistsException ex) {
            Logger.getLogger(entityClass.getName())
                    .log(Level.INFO, ex.getMessage());
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity(ex.getMessage()).build();
        } catch (Exception ex) {
            Logger.getLogger(entityClass.getName())
                    .log(Level.WARNING, ex.getMessage());
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity(ex.getMessage()).build();
        }
    }

    private static class AbstractList implements ParameterizedType {

        Type type;

        public AbstractList(Type type) {
            this.type = type;
        }

        @Override
        public Type[] getActualTypeArguments() {
            return new Type[]{type};
        }

        @Override
        public Type getRawType() {
            return List.class;
        }

        @Override
        public Type getOwnerType() {
            return AbstractResource.class;
        }

    }

}

当我不使用抽象类但将所有内容直接放在GroupController和GroupResource中时,代码可以正常工作。然而,我有很多实体类和方法(GET,PUT,DELETE),我想保持代码可管理。 当我将其更改为public Response post(T entity)时,public Response post()上出现错误,不再有任何警告。 如何从我的抽象类中告诉JAX-RS在扩展类中类型不是T(如错误所示)但是键入Group?

1 个答案:

答案 0 :(得分:3)

您的课程定义如下:

@Path("group")
public class GroupResource extends AbstractResource {

不应该这样定义吗?

@Path("group")
public class GroupResource extends AbstractResource<Group> {