我正在尝试测试POST,无论我做什么我都会得到<div *ngFor="let item of arrayOfObject "></div>
我的控制器:
java.lang.AssertionError: Content type not set
和我的测试:
@RestController
@RequestMapping("/artistas")
public class ArtistaEndpoint {
private final ArtistaService artistaService;
private final ArtistaMapper artistaMapper;
private final AlbumService albumService;
@CrossOrigin(origins = "http://localhost:8080")
@PostMapping
public ResponseEntity<Void> post(@Valid @RequestBody Artista artista) {
artista = artistaService.save(artista);
ArtistaDto artistaDto = artistaMapper.toDtoCompleto(artista);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(artistaDto.getId()).toUri();
if(!artista.getAlbuns().isEmpty()) {
Set<Album> albuns = artista.getAlbuns();
for(Album album: albuns) {
album.setArtista(artista);
albumService.save(album);
}
}
return ResponseEntity.created(uri).build();
}
}
httpservlet的响应:
@Test
public void salvarArtistaSemAlbum() throws Exception {
Artista adicionado = new Artista();
adicionado.setId(1L);
adicionado.setNome("Banda Eva");
adicionado.setDescricao("Viva o carnaval");
when(artistaService.save(Mockito.any(Artista.class))).thenReturn(adicionado);
mockMvc.perform(MockMvcRequestBuilders.post("/artistas")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)and
.content(TestUtil.asJsonString(adicionado)))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.nome", is("Banda Eva")))
.andExpect(jsonPath("$.descricao", is("Viva o carnaval")));
verify(artistaService, times(1)).save(Mockito.any(Artista.class));
verifyNoMoreInteractions(artistaService);
assertNull(adicionado.getId());
assertThat(adicionado.getNome(), is("Banda Eva"));
assertThat(adicionado.getDescricao(), is("Viva o carnaval"));
}
我已经问了一个问题,但错误是另一个,我可以解决。我不明白为什么它会返回201创建的内容并且没有内容。闻起来像我没有的注解,但我已经审核成功了。 对不起我的英语,谢谢您的帮助。
编辑-
我以为问题解决了,但是我错了
答案 0 :(得分:0)
在控制器方法之上添加@ResponseBody
。
OR
@PostMapping(produces = {MediaType.APPLICATION_JSON_VALUE} )
答案 1 :(得分:0)
您可以直接在PostMapping
批注中添加内容类型:
@PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
答案 2 :(得分:0)
您可以添加@RequestMapping
批注以将RestController
中的内容类型指定给相应的方法。
类似的东西:
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
。
已更新:
此新注释是@RequestMapping
和RequestMethod.POST
的快捷方式:
@PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes=MediaType.APPLICATION_JSON_UTF8_VALUE)
答案 3 :(得分:0)
问题解决了,我的问题在控制器上,因为它返回一个空值:
我的新控制器如下:
public class ArtistaEndpoint {
private final ArtistaService artistaService;
private final ArtistaMapper artistaMapper;
private final AlbumService albumService;
@CrossOrigin(origins = "http://localhost:8080")
@PostMapping
public ResponseEntity<ArtistaDto> post(@Valid @RequestBody Artista artista) {
artista = artistaService.save(artista);
ArtistaDto artistaDto = artistaMapper.toDtoCompleto(artista);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(artistaDto.getId()).toUri();
if(!artista.getAlbuns().isEmpty()) {
Set<Album> albuns = artista.getAlbuns();
for(Album album: albuns) {
album.setArtista(artista);
albumService.save(album);
}
}
return ResponseEntity.created(artistaDto).body();
}
}
感谢所有人