我正在尝试编写一个简单的测试类来模拟通过POST
方法创建客户的 RESTful Web 服务。以下失败assertEquals
,我收到400 Bad Request
回复。我不能使用调试器来观察堆栈跟踪。但是控制台告诉我以下...
信息:已启动侦听器绑定到 [localhost:9998]
信息:[HttpServer] 已启动。
public class SimpleTest extends JerseyTestNg.ContainerPerMethodTest {
public class Customer {
public Customer() {}
public Customer(String name, int id) {
this.name = name;
this.id = id;
}
@JsonProperty("name")
private String name;
@JsonProperty("id")
private int id;
}
@Override
protected Application configure() {
return new ResourceConfig(MyService.class);
}
@Path("hello")
public static class MyService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public final Response createCustomer(Customer customer) {
System.out.println("Customer data: " + customer.toString());
return Response.ok("customer created").build();
}
}
@Test
private void test() {
String json = "{" +
"\"name\": \"bill\", " +
"\"id\": 4" +
"}";
final Response response = target("hello").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(json));
System.out.println(response.toString());
assertEquals(response.getStatus(), 200);
}
}