Java JAX-RS、JAXB实现,用于创建简单对象和测试
我正在学习使用JAX-RS和JAXB实现web服务,但是我无法让它工作。其想法是拥有一个web服务,可以创建一个客户(使用客户名称)并将其存储在哈希映射中。我也尝试创建测试,但测试失败,出现错误 javax.ws.rs.ProcessingException:无法调用请求 位于org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke (ApacheHttpClient4Engine.java:287)在 org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke (ClientInvocation.java:407) 在org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post上 (ClientInvocationBuilder.java:195) 在bloggetest.addCustomer(StoreTest.java:65) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)Java JAX-RS、JAXB实现,用于创建简单对象和测试,java,web-services,junit,jaxb,jax-rs,Java,Web Services,Junit,Jaxb,Jax Rs,我正在学习使用JAX-RS和JAXB实现web服务,但是我无法让它工作。其想法是拥有一个web服务,可以创建一个客户(使用客户名称)并将其存储在哈希映射中。我也尝试创建测试,但测试失败,出现错误 javax.ws.rs.ProcessingException:无法调用请求 位于org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke (ApacheHttpClient4Engine.java:287)在 org.
故障跟踪显示“由以下原因引起:javax.ws.rs ProcessingException: 找不到内容类型应用程序/xml类型的编写器:store.domain.Customer 我不太明白 下面是我目前得到的,我正在使用“return Singleton”创建store类的实例: 商店类别:
public class Store {
private Map<String, Customer> _customer;
public Store() {
_customer = new ConcurrentHashMap <String,Customer>();
}
//assume the incoming http request contains customer name
@Post
@Consumes("application/xml")
public Response createCustomer (Customer customer){
_customer.put(customer.getName(), customer);
return Response.created( URI.create("/customers/" + customer.getName()))
.build();
}
}
我觉得我在这里遗漏了一些重要的观点,但我真的不明白我在注释或其他方面是怎么做错的。有人能帮忙吗?非常感谢 我也面临同样的问题。在我的服务方法上使用@products注释帮助我解决了这个问题。希望这对您有所帮助。谢谢您的回复!我以为@products是为了获取请求?你能解释一下我应该把它放在哪个方法上吗?比如“@Consumer”告诉你的方法你的方法将使用什么内容类型同样“@products”告诉你的方法它将返回什么内容类型。在createCustomer方法中使用它。我认为您是在返回xml作为响应,所以将其设置为“application/xml”。您是否在
Customer
类上有@XmlRootElement
,并且Customer
类也有一个无参数构造函数?需要@XmlRootElement
。同样,如果没有no-arg,在服务器端,JAXB将无法构造它,您将得到另一个异常。您在客户端中面临的当前异常。因此请求甚至没有到达服务器。这两个路径段services/store
来自哪里?Store
类是否具有@Path(“/Store”)
?应用程序
子类是否具有@ApplicationPath(“/services”)
,或者是否在web.xml中配置了/services
?您是否能够从浏览器访问GET
端点?这应该会让您了解所需的URL。只需在Store
类中添加一个不同的@GET
方法,并尝试从一个简单的浏览器访问它。这也是打字错误吗?因为它应该是大写的。如果你复制并粘贴了它,我不确定你从哪里得到了lowerase@Post
如果你说的是我认为你在说的话,即“请求成功,但当你试图访问创建的uri时,你会得到一个404”,那么你就不知道它是如何工作的。服务器上没有动态创建的终结点。您需要有另一个端点,使用@GET@Path(“/{id}”)
,您需要根据id查找名称,然后返回对象。请尝试类似于post方法的方法。可能uri创建不正确。在客户机代码中,我不会直接转换到Customer
,而是返回Response
。这样,您至少可以对状态代码等进行一些调试
public class StoreTest {
private static final String WEB_SERVICE_URI = "http://localhost:10000/services/store";
private static Client _client;
@BeforeClass
public static void setUpClient() {
_client = ClientBuilder.newClient();
}
@Before
public void reloadServerData() {
Response response = _client
.target(WEB_SERVICE_URI).request()
.put(null);
response.close();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
@AfterClass
public static void destroyClient() {
_client.close();
}
@Test
public void addCustomer() {
Customer BartSimpsons = new Customer ("BartSimpsons");//create a new customer with name
Response response = _client
.target(WEB_SERVICE_URI).request()
.post(Entity.xml(BartSimpsons));
String location = response.getLocation().toString();
response.close();
Customer BartCreated = _client.target(location).request()
.accept("application/xml").get(Customer.class);
//check if the Customer created by the service has the same name with the original customer
assertEquals(BartSimpsons.getName(), BartCreated.getName());
}
}