我收到以下异常:
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)
我的测试如下所示:
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();
}
BaseTest为:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@AutoConfigureMockMvc
public class BaseTest {
@Autowired
protected MockMvc mockMvc;
}
@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();
}
}
@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
);
}
}
我认为您应该将@EnableWebMVC添加到您的TestConfiguration中。
@Configuration
@ComponentScan(
basePackages = "com.app",
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = App.class)
}
)
@EnableWebMvc
public class TestConfiguration {
我的控制器如下所示: 我已经尝试将“consumes”更改为将APPLICATION_OCTET_STREAM_VALUE删除为MULTIPART_FORM_DATA_VALUE,也尝试删除它,但这些都没有帮助。 如果你需要更多的信息,请告诉我。谢了。
我的服务器资源: 我的客户资源: 我的Build.Gradle文件:
@PostMapping public UserResponse createUser(@RequestBody UserRequest userDetail){ 这是我收到的错误代码 } 我尝试了很多不同的方法仍然没有得到解决方案这是来自邮递员的图片来自邮递员的图片
我以前有ElasticSearch 5.2,刚刚升级到6.0。 我试图创建一个索引模板以下指南在这里,但得到了错误 我的问题是
问题内容: 我写了下面的@Controller方法 请求失败并出现以下错误 [PS:泽西岛要友好得多,但鉴于此处的实际限制,现在无法使用它] 问题答案: 问题在于,当我们使用application / x-www-form-urlencoded时,Spring不会将其理解为RequestBody。因此,如果要使用它,则必须删除@RequestBody批注。 然后尝试以下操作:
问题内容: 基于Spring @Controller对x-www-form-urlencoded的问的答案 我写了下面的@Controller方法 失败的请求因以下错误 [PS:Jersey要友好得多,但鉴于这里的实际限制,现在无法使用它] 问题答案: 问题在于,当我们使用 application / x-www-form-urlencoded时 ,Spring不会将其理解为RequestBody