[英]How to set consumes and produces of rest controller in mockmvc using Spring boot?
I have a controller like this;我有这样一个 controller;
@RestController
@RequestMapping(GA)
@RequiredArgsConstructor
public class GaController {
private final GaService service;
@PostMapping(value = INITIALIZE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<GaDTO> initialize() {
return status(CREATED).body(service.initialize());
}
@GetMapping(value = FIND, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<GaDTO> find(@PathVariable String id) {
return ok().body(service.find(id));
}
@PutMapping(value = PLAY, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<GaDTO> play(@Valid @RequestBody Request request) {
return ok().body(service.play(request));
}
@DeleteMapping(value = DELETE)
public ResponseEntity<Void> delete(@PathVariable String id) {
service.delete(id);
return status(NO_CONTENT).build();
}
}
And in my unit test, I used @WebMvcTest
and @MockMvc
but I don't know how to set produces
and consumes
configurations in mockMvc
.在我的单元测试中,我使用@WebMvcTest
和@MockMvc
但我不知道如何在mockMvc
中设置produces
和consumes
配置。
Here's an example;这是一个例子;
@Test
public void initialize_success() throws Exception {
when(gaService.initialize()).thenReturn(gaDTO);
mockMvc.perform(post(GA + INITIALIZE)
.contentType(APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id", is(gaDTO.getId())));
}
Is this true?这是真的? I mean, .contentType(APPLICATION_JSON)
refers to produces
or consumes
?我的意思是, .contentType(APPLICATION_JSON)
指的是produces
还是consumes
?
I don't know if it's the case for SpringBoot, but with JPA you should have the Annotations @Consumes and @Produces which you can put above a method.我不知道 SpringBoot 是否属于这种情况,但是对于 JPA,您应该具有可以放在方法上方的注释 @Consumes 和 @Produces。
For example you can put @Consumes(MediaType.APPLICATION_JSON) or @Produces(MediaType.TEXT_PLAIN)例如,您可以放置 @Consumes(MediaType.APPLICATION_JSON) 或 @Produces(MediaType.TEXT_PLAIN)
given that MediaType is available as well.鉴于 MediaType 也可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.