我尝试了2天才找到关于这个问题的东西,但仍然没有得到它。我让我的Maven-Project在Wildfly上运行。
REST的代码:
@Override
@GET
@Path("{id}")
// @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Customer getOverId(@PathParam("id") String id) {
logger.info("put in " + this.getClass().getName() + " over id " + id);
// if (id != null) {
// Customer object = service.loadOneCustomer(new Integer(id));
// logger.info("found in " + this.getClass().getName());
// return Response.ok(object).build();
// }
Customer customer = service.loadOneCustomer(new Integer(id));
// logger.info("nix found");
if(customer == null) {
throw new NotFoundException("No customer found with the matching ID: " + id);
}
logger.info("Customer found: " + customer.getCustomerNumber());
// return Response.status(Status.BAD_REQUEST).build();
return customer;
}
客户实现:
public Response readCustomer(String id){
log.info("Starting: Rest get a Customer with ID: " + id);
log.info(this.URL);
this.customerWebTarget = this.client.target(this.URL).path(id);
Response response = this.customerWebTarget.request().buildGet().invoke();
// TODO Customer cast application_xml auf Customer? Customer customer = response.readEntity(Customer.class);
Customer customer = response.readEntity(Customer.class);
log.info("Ending: Rest invoque a Customer with ID:" + customer.getCustomerNumber());
// System.out.println("Ending: Rest get a Customer with ID: " + response.readEntity(String.class));
return response;
}
J-Unit测试:
@Test
public void testGetCustomerById() throws Exception {
Response response = this.customerRestClient.readCustomer("112");
System.out.println("--->" + response.getStatus());
Assert.assertTrue(response.getStatus() == 200);
}
一切正常,直到我尝试从XML获取Java-Object(Customer customer = response.readEntity(Customer.class);)
我错过了什么。我的意思是,我读取了xml-File并查看其中的每个数据...为什么我不能将它转换为Java-Object?
我总是得到这个错误:
Javax.ws.rs.ProcessingException: Unable to find a MEssageBody of content-type-application/xml and type class de.....model.Customer
答案 0 :(得分:0)
没有看到Customer
类,很难说,但很可能缺少部分或全部JAXB注释。特别是,您需要@XmlRootElement
注释。
答案 1 :(得分:-1)
请您发布客户课程。它被正确注释了吗? 还要重新添加@Produces。