gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:12:38 26 4
gpt4 key购买 nike

您好,我在尝试向 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com