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

标签 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    

}

Controller :

@Stateless
public class GroupController extends AbstractController<Group> {

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

}

它扩展了抽象 Controller :

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() 时,错误发生在 public Response post(T entity) 上,不再有警告。 我如何从我的抽象类中告诉 JAX-RS,在扩展类中类型不是 T(如错误提示的那样)而是类型 Group?

最佳答案

你的类定义如下:

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

不应该这样定义吗?

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

关于java - 无法执行 : javax. ws.rs.NotSupportedException:找不到类型的消息正文阅读器:T 内容类型:application/xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838719/

相关文章:

java - Spring Batch 中的复制作业

java - 无法从 Hazelcast 队列检查对象

java - IntelliJ IDEA 插件 : Jump to declaration for custom xml

java - saxon-xpath-9.1.0.8.jar!/META-INF/services/javax.xml.xpath.XPathFactory :2: Illegal configuration-file syntax

java - 如何处理带有 MixedContent 数据的 JAXB ComplexType?

java - 是否有用于包装 Text/XML/JSON 数据服务的语言中立 API 生成器?

java - 如何用动态变化的简单数组创建多维数组?

java - 在java中获取eth0接口(interface)的IP地址只返回IPv6地址而不是IPv4

ios - Azure Blob 授权 header

java - Camel CXF休息: Setup then poll