我正在尝试获取JAX RS资源以返回带有JSON对象的响应。当我通过资源类中的println()调用显示响应属性时,我看到MediaType被正确设置为“application / json”,有一个实体与具有预期类型(SalesOrder),状态(状态的响应)相关联.FOUND)并且响应对象是OutboundJaxrsResponse的实例。但不知何故,当浏览器(或Jersey客户端)收到响应时,这些响应属性似乎被“替换”,结果就是HTTP 500内部服务器错误。我的SalesOrder类使用@XmlRootElement注释。
我的资源如下:
@GET
@Path("{orderUid}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getSalesOrder(@PathParam("orderUid") Integer orderUid) {
Response response = null;
System.out.println("Entering SalesOrderResource getSalesOrder()...");
SalesOrder retrievedSalesOrder = salesOrderService.retrieveSalesOrder(orderUid);
System.out.println("Service called and found salesOrder Uid: " + retrievedSalesOrder.getSalesOrderUid());
if (retrievedSalesOrder != null) {
System.out.println("SalesOrder found with UID: " + retrievedSalesOrder.getSalesOrderUid());
response = Response.status(Status.FOUND).entity(retrievedSalesOrder).type(MediaType.APPLICATION_JSON).build();
// The following readEntity call results in a Javax.ejb.EJBException ???
// SalesOrder fetched = response.readEntity(SalesOrder.class);
} else {
response = Response.status(Status.NOT_FOUND).header("x-reason", "Order cannot be found").build();
}
System.out.println("Response status: " + response.getStatus());
System.out.println("Response status info: " + response.getStatusInfo());
System.out.println("Response class: " + response.getClass());
System.out.println("Response length: " + response.getLength());
System.out.println("Response media type: " + response.getMediaType());
System.out.println("Response entity: " + response.getEntity());
return response;
}
...在运行时产生以下结果:
2015-04-12T18:08:21.803-0600 |信息:回复状态:302 2015-04-12T18:08:21.803-0600 |信息:响应状态信息:找到 2015-04-12T18:08:21.803-0600 |信息:响应类:class org.glassfish.jersey.message.internal.OutboundJaxrsResponse 2015-04-12T18:08:21.803-0600 |信息:响应时长:-1 2015-04-12T18:08:21.803-0600 |信息:响应媒体类型:application / xml 2015-04-12T18:08:21.803-0600 |信息:响应实体:business.salesOrderMgmt.entity.SalesOrder@5e49cadd
SalesOrder实体定义为:
@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@JsonIgnoreProperties({"subTotal", "userAccount"})
@Table(name="sales_order")
@NamedQuery(name="SalesOrder.findAll", query="SELECT s FROM SalesOrder s")
public class SalesOrder implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="SALES_ORDER_UID")
private int salesOrderUid;
@Column(name="ORDER_NUMBER")
private String orderNumber;
@Column(name="BILL_TO_CITY")
private String billToCity;
@Column(name="BILL_TO_FIRST_NAME")
private String billToFirstName;
@Column(name="BILL_TO_LAST_NAME")
private String billToLastName;
@Column(name="BILL_TO_STATE")
private String billToState;
@Column(name="BILL_TO_STREET_NAME")
private String billToStreetName;
@Column(name="BILL_TO_STREET_NUMBER")
private String billToStreetNumber;
@Column(name="BILL_TO_UNIT_NUMBER")
private String billToUnitNumber;
@Column(name="BILL_TO_ZIP_CODE")
private int billToZipCode;
@Column(name="CREDIT_CARD_CSV")
private int creditCardCsv;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREDIT_CARD_EXPIRATION_DATE")
private Date creditCardExpirationDate;
@Column(name="CREDIT_CARD_NUMBER")
private String creditCardNumber;
@Column(name="CREDIT_CARD_TYPE")
private String creditCardType;
@Column(name="EMAIL_ADDRESS")
private String emailAddress;
@Column(name="NAME_ON_CREDIT_CARD")
private String nameOnCreditCard;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="SALES_ORDER_DATE")
private Date salesOrderDate;
@Column(name="SALES_ORDER_STATUS")
private String salesOrderStatus;
@Column(name="SHIP_TO_CITY")
private String shipToCity;
@Column(name="SHIP_TO_STATE")
private String shipToState;
@Column(name="SHIP_TO_STREET_NAME")
private String shipToStreetName;
@Column(name="SHIP_TO_STREET_NUMBER")
private String shipToStreetNumber;
@Column(name="SHIP_TO_UNIT_NUMBER")
private String shipToUnitNumber;
@Column(name="SHIP_TO_ZIP_CODE")
private int shipToZipCode;
@Column(name="PROMO_CODE")
private String promoCode;
//Calculated and persisted for data retrieval performance
@Column(name="SUB_TOTAL")
private BigDecimal subTotal;
@Column(name="DISCOUNT")
private BigDecimal discount;
@Column(name="SALES_TAX")
private BigDecimal salesTax;
@Column(name="SHIPPING")
private BigDecimal shipping;
@Column(name="TOTAL")
private BigDecimal total;
@Version
@Column(name="LAST_UPDATED_TIME")
private Timestamp lastUpdatedTime;
//bi-directional many-to-one association to UserAccount
@ManyToOne
@JoinColumn(name="USER_ACCOUNT_UID")
private UserAccount userAccount;
@OneToMany(targetEntity=SalesOrderLine.class, mappedBy = "salesOrder", cascade = CascadeType.ALL)
private List<SalesOrderLine> lineItems;
public SalesOrder() {
}
......吸气者和二传手
我的POM中的依赖关系包括:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.5.2</version>
</dependency>
请求(http://localhost:8080/[myapp]/resources/salesOrders/13)不仅会导致HTTP 500内部服务器错误,而且当我尝试在资源中调用response.readEntity(SalesOrder.class)时,Glassfish服务器日志会显示javax.ejb。由java.lang.IllegalStateException引起的EJBException警告:org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:145)上的出站消息不支持该方法。
我认为SalesAXr对象的JAXB编组存在问题,但我无法确定根本原因。如果我只是尝试以下操作,我仍然会收到HTTP 500内部服务器错误,表明既没有XML也没有JSON编组,但我认为这是内置于最新版本(Glassfish 4,JAX-RS 2)?
有什么想法吗?
答案 0 :(得分:1)
我想出了问题并将其张贴在这里,以造福他人。 1)我在资源方法中添加了以下内容: @Consumes(MediaType.TEXT_PLAIN) 2)步骤#1在运行时导致以下错误: 在对象图中检测到循环。这将导致无限深度的XML 3)错误导致我将@XmlTransient添加到与OneToMany,ManyToMany和ManyToOne中的其他实体相关的所有实体字段。
现在我能够以XML和JSON返回响应。
答案 1 :(得分:0)
我遇到过类似的问题。当然,请检查您的日志是否显示如下内容:
SEVERE:找不到媒体类型= application / json的MessageBodyWriter, type = class SalesOrder,genericType = class SalesOrder。
如果是这样,问题是您没有注册Jackson(或其他JSON编写器提供商)。有关说明,请参阅Jersey文档的此部分:
https://jersey.java.net/documentation/latest/media.html#d0e7857
就我而言,我选择了杰克逊。我在pom.xml中添加了以下内容。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
然后我必须注册提供者:
public class Application extends ResourceConfig {
public Application() {
packages(<your ws package>);
register(JacksonFeature.class);
}
}