0
我寫帖子的方法測試了我的資源獲取自定義消息出來拋出自定義異常的
@POST
@Path("/api")
@Timed
@UnitOfWork
@Consumes(MediaType.APPLICATION_JSON)
public Person createPerson(Person p, @Context UriInfo uriInfo) {
//do something here or throw 400 exception
}
這裏的一個是測試
@Test
public void testThrows400() throws Exception {
thrown.expect(400Exception.class);
Response response = resources.client().target("/api").request(MediaType.APPLICATION_JSON).post(Entity.entity(person, MediaType.APPLICATION_JSON_TYPE));
assertEquals(response.getStatus(), 400);
assertEquals(response.getStatusInfo(), Status.BAD_REQUEST);
}
這按預期工作。但有沒有辦法可以返回我發送異常時發送的自定義消息?
這裏是異常類
public class InvalidNameException extends WebApplicationException
{
private static final long serialVersionUID = 1L;
public InvalidNameException(URI location)
{
this(location, null);
}
public InvalidNameException(URI location, Object entity) {
super(Response.status(Status.BAD_REQUEST).location(location).entity("Invalid name, please use a valid name").build());
}
}