我正在運行的Java 8 Dropwizard啓用你的資源驗證0.7在Dropwizard
我無法得到驗證,在Dropwizard
在我的資源方面的工作,我有以下豆
@Data //lombok anntation
@JsonRootName(value="CreateNewGame")
@XmlRootElement
public class CreateNewGameDTO {
@NotEmpty
public String name;
@NotNull
private GameType type;
@NotBlank
private String username;
@NotNull
@Min(1)
private Integer numOfPlayers;
}
而在我的資源類中,我有以下方法
@POST
@Timed
public Response createGame(@Valid CreateNewGameDTO dto) { ... }
My JUnit test
@ClassRule
public static final DropwizardAppRule<CivBoardGameRandomizerConfiguration> RULE =
new DropwizardAppRule<CivBoardGameRandomizerConfiguration>(CivBoardgameRandomizerApplication.class, "src/main/resources/config.yml");
@Test
public void createGameShouldFailBecauseOfMissingUsername() throws Exception {
List<NewCookie> cookies = performLogin();
assertThat(cookies.size()).isEqualTo(2);
Client client = Client.create();
CreateNewGameDTO dto = new CreateNewGameDTO();
dto.setNumOfPlayers(4);
dto.setUsername(null); //Username NULL, so it should throw exception
dto.setName("PBF WaW");
dto.setType(GameType.WAW);
ObjectMapper mapper = new ObjectMapper();
String dtoAsJSon = mapper.writeValueAsString(dto);
URI uri = UriBuilder.fromPath(String.format(BASE_URL + "/game", RULE.getLocalPort())).build();
ClientResponse response = client.resource(uri)
.type(MediaType.APPLICATION_JSON)
.cookie(cookies.get(0))
.cookie(cookies.get(1))
.entity(dtoAsJSon)
.post(ClientResponse.class);
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201);
URI location = response.getLocation();
assertTrue(location.getPath().matches(".*civilization/game/.*"));
}
當我創建單元測試撥打這個資源,我得到以下例外
ERROR [2014-10-01 11:03:39,515] com.sun.jersey.spi.container.ContainerResponse: A message body writer for Java class io.dropwizard.jersey.validation.ValidationErrorMessage, and Java type class io.dropwizard.jersey.validation.ValidationErrorMessage, and MIME media type application/xml was not found.
The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
application/xml ->
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App
我還沒有做任何事情來「註冊」驗證。我以爲dropwizard是自動完成的。我還沒有在文檔中找到任何內容。 我正在使用Dropwizard的0.7版本
當你sk ip lombok部分? – 2014-10-01 14:43:40
@JanGalinski當然沒有。龍目島生成樣板代碼。它不應該影響這一點,也不會。仍然得到相同的錯誤 – 2014-10-01 16:03:34
我正在使用Java 8,也許這就是原因。雖然,我無法想象爲什麼。我已經加入我的JUnit測試也 – 2014-10-01 16:05:00