Skip to content

I use getContentAsString() and mockMvc seems to return my content as ISO 8859-1. #23851

@chrisaige

Description

@chrisaige

I use getContentAsString() and mockMvc seems to return my Content as ISO 8859-1.
Before (with Spring Boot 2.1.8) it was proper UTF-8

When I add a produces to my Endpoint like @GetMapping(value = "/test", produces={"application/json; charset=UTF-8"}) it works as indented.
But I dont want todo this on our big project with tons of endpoints

Example:

@RestController
@RequestMapping("test")
public class TestClass {
  @GetMapping
  public List<String> test() {
    return List.of("AEß");
  }
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
@AutoConfigureMockMvc()
@ActiveProfiles("test")
@Transactional
public class TestClassTest {
  @Autowired
  private ObjectMapper objectMapper;
  @Autowired
  private MockMvc mvc;
  @Test
  public void test() throws Exception {
    var mvcResult = mvc.perform(MockMvcRequestBuilders
      .get("/test")
      .contentType(MediaType.APPLICATION_JSON)
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(MockMvcResultMatchers.status().isOk())
      .andReturn();

    assertEquals(List.of("AEß"), objectMapper.readValue(mvcResult.getResponse().getContentAsString(),
      new TypeReference<List<String>>() {
      }));
  }
}

Fails with:

java.lang.AssertionError: 
Expected :[AEß]
Actual   :[AE�]

Originally posted by @chrisaige in #23622 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions