mardi 2 janvier 2018

No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer

I need to send to the controller

@PostMapping(value = "/{id}/contributions/photos")
@ResponseStatus(HttpStatus.CREATED)
public
ResponseEntity<Void> createPhotoContribution(
        @ApiParam(value = "The movie ID", required = true)
        @PathVariable("id") final Long id,
        @ApiParam(value = "The contribution", required = true)
        @RequestBody @Valid final ContributionNew<Photo> contribution
) {

DTO object

@Getter
public class ContributionNew<T extends MovieInfoDTO> {
    private List<T> elementsToAdd;
    private Map<Long, T> elementsToUpdate;
    private Set<Long> idsToDelete;
    private Set<String> sources;
    private String comment;
}

@Getter
@JsonDeserializer(builder = Photo.Builder.class)
public class Photo extends MovieInfoDTO {
    private MultipartFile photo;

    private Builder ...
}

as it can not be done with any Postman or Swagger tool, then you need to write a test. I scraped something, only during testing throws it out

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.common.dto.movie.request.ContributionNew["elementsToAdd"]->java.util.ArrayList[0]->com.common.dto.movie.Photo["photo"]->org.springframework.mock.web.MockMultipartFile["inputStream"])

The test itself looks as follows

@Test
public void testCreatePhotoContribution() throws Exception {
    ContributionNew<Photo<MultipartFile>> contribution = new ContributionNew<>();
    Path path = Paths.get("C:\\Users\\Jonatan\\Pictures\\2.png");
    MockMultipartFile multipartFile = new MockMultipartFile("photo", "C:\\Users\\Jonatan\\Pictures\\2.png",
            "image/png", Files.readAllBytes(path));
    Photo.Builder<MultipartFile> photoBuilder = new Photo.Builder<>(
            multipartFile
    );
    contribution.getElementsToAdd().add(photoBuilder.build());

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    mockMvc
            .perform(fileUpload("/api/v1.0/movies/{id}/contributions/photos", 1)
                .contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(contribution)))
            .andExpect(status().isCreated());
}

Aucun commentaire:

Enregistrer un commentaire