2017-06-14 4 views
4

Je l'exception suivante:MockMvc HttpMediaTypeNotSupportedException: Type de contenu 'application/json' non pris en charge

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported 

    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:653) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:612) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:361) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167) 
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 

Mon test se présente comme suit:

public class AvailabilityControllerTest extends BaseTest { 
    @Test 
    public void createAvailability() throws Exception { 
     final String createAvailabilityEndPoint = "/api/v4/companies/123/availabilities"; 
     final String responseName = "availabilityResponseDTO"; 

     AvailabilityDTO availabilityDTO = new AvailabilityDTO(); 

     MvcResult mvcResult = mockMvc.perform(
       MockMvcRequestBuilders.post(createAvailabilityEndPoint) 
         .contentType(MediaType.APPLICATION_JSON_VALUE) 
         .accept(MediaType.APPLICATION_JSON_VALUE) 
         .content(new ObjectMapper().writeValueAsString(availabilityDTO))) 
       .andExpect(MockMvcResultMatchers.status().isCreated()) 
       .andReturn(); 
    } 

Avec BaseTest comme:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = TestConfiguration.class) 
@AutoConfigureMockMvc 
public class BaseTest { 

    @Autowired 
    protected MockMvc mockMvc; 

} 

TestConfiguration ressemble à ceci:

@Configuration 
@ComponentScan(
     basePackages = "com.app", 
     excludeFilters = { 
       @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = App.class) 
     } 
) 
public class TestConfiguration { 

    @Bean 
    public ErrorAttributes errorAttributes() { 
     return new DefaultErrorAttributes(); 
    } 
} 

AvailabilityController ressemble à ceci:

@RestController 
@RequestMapping("/api/v4/companies") 
public class AvailabilityController { 

    public static final String ACCEPT_APP_JSON = "Accept=" + AcceptableMediaType.APPLICATION_JSON; 

    @Autowired 
    private AvailabilityFacade availabilityFacade; 

@RequestMapping(value = "/{companyId}/employees/{employeeId}/availabilities", method = RequestMethod.GET) 
    public List<AvailabilityEventResponseDTO> getUserAvailabilities(@PathVariable String companyId, 
                    @PathVariable String employeeId) { 
     return availabilityFacade.getUserAvailabilities(employeeId); 
    } 

    @RequestMapping(value = "/{companyId}/availabilities", method = RequestMethod.POST, headers = ACCEPT_APP_JSON, consumes = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<AvailabilityResponseDTO> createAvailability(@PathVariable String companyId, 
                     @Valid @RequestBody AvailabilityDTO availabilityDTO) { 
     return new ResponseEntity<>(
       availabilityFacade.createAvailability(companyId, availabilityDTO), 
       HttpStatus.CREATED 
     ); 
    } 
} 

Fondamentalement, la requête GET fonctionne avec MockMvc mais le POST ne fonctionne pas et renvoie cette HttpMediaTypeNotSupportedException. J'ai essayé d'ajouter et supprimer des en-têtes pour accepter et le type de contenu à la fois dans la demande et le contrôleur et cela ne semble pas fonctionner.

Ce problème semble être lié à Spring integration test with mockmvc throws org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported mais la suppression des excludeFilters dans TestConfiguration dans ce cas ne permet pas au contexte d'être interprété par Spring. En outre, je ne suis pas sûr de ce que Karl R veut dire par "inclure un serverruntime sur mon classpath".

Toute aide est appréciée.

Répondre

1

Je pense que vous devriez ajouter @EnableWebMvc à votre TestConfiguration.

@Configuration 
@ComponentScan(
    basePackages = "com.app", 
    excludeFilters = { 
      @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, 
       classes = App.class) 
    } 
) 
@EnableWebMvc 
public class TestConfiguration {