gpt4 book ai didi

java.lang.AssertionError : Content type not set

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:40 24 4
gpt4 key购买 nike

我正在尝试测试 POST,无论我做什么,我都会收到 java.lang.AssertionError: Content type not set

我的 Controller :

@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();
}

}

和我的测试:

    @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"));

}

httpservlet 的响应:

    MockHttpServletResponse:
Status = 201
Error message = null
Headers = {Location=[http://localhost/artistas/1]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = http://localhost/artistas/1
Cookies = []

我已经做了一个问题,但错误是另一个,我可以解决。我不明白为什么它返回 201 Created 并且没有内容正文。闻起来像一些我没有的注释,但我已经审阅过但没有成功。对我的英语很抱歉,感谢您的帮助。

编辑-

我以为问题已经解决了,但我错了

最佳答案

您可以直接在 PostMapping 注释中添加内容类型:

@PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

关于java.lang.AssertionError : Content type not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51305680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com