[英]Spring boot MockMVC test not using custom gson type mapper for deserialization
I have the following type mapper: 我有以下类型映射器:
public class LocaleTwoWaySerializer implements JsonSerializer<Locale>, JsonDeserializer<Locale> {
@Override
public JsonElement serialize(final Locale src, final Type typeOfSrc, final JsonSerializationContext context) {
return src == null ? JsonNull.INSTANCE : new JsonPrimitive(src.getLanguage());
}
@Override
public Locale deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
return json == null || JsonNull.INSTANCE.equals(json) ? null : new Locale(json.getAsString());
}
}
My GsonConfig: 我的GsonConfig:
@Configuration
public class GsonConfig {
private static Gson GSON = generateStaticBuilder().create();
private static GsonBuilder generateStaticBuilder() {
return new GsonBuilder()
.registerTypeAdapter(Locale.class, new LocaleTwoWaySerializer())
.enableComplexMapKeySerialization()
.setPrettyPrinting()
}
@Bean
public Gson getGsonInstance() {
return GSON;
}
}
My Web Config: 我的网络配置:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
@ConditionalOnMissingBean
@Autowired
public GsonHttpMessageConverter gsonHttpMessageConverter(final Gson gson) {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson);
return converter;
}
}
My Test: 我的测试:
@SpringBootTest(webEnvironment = WebEnvironment.MOCK )
@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc
public class MyTestClassIT {
@Autowired
private MockMvc mvc;
@Autowired
private Gson gson;
@Test
public void updateLocale() throws Exception {
Locale locale = Locale.ITALIAN;
mvc.perform(patch("/mypath").contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(locale))).andExpect(status().isOk());
}
}
The api i am testing has this signature: 我正在测试的api具有以下签名:
@PatchMapping(path = "myPath")
public ResponseEntity<Void> updateLocale(@RequestBody Locale preferredLocale)
I have placed breakpoints and can verify that my GsonHttpMessageConverter is being constructed, and serialization in my test using the autowired GSON instance directly works fine (using my typeAdapter) however I am getting a 400 bad request, As the type adapter is not used to deserialize. 我已放置断点并可以验证是否正在构造我的GsonHttpMessageConverter,并且在使用自动连接的GSON实例的测试中进行序列化直接可以正常工作(使用我的typeAdapter),但是我收到一个400错误的请求,因为类型适配器不用于反序列化。
What is missing please? 请缺少什么?
Are you sure you are not getting a 404
instead? 您确定没有收到
404
吗? I recreated your project here https://github.com/Flaw101/gsonconverter and verified it was working except with your setup it's a 404
because of capitalization in the path
param which is not in the test. 我在这里https://github.com/Flaw101/gsonconverter重新创建了您的项目,并验证了它的正常工作,但由于
path
参数中的大写而不是测试,因此您的设置是404
。
mvc.perform(patch("/mypath").contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(locale))).andExpect(status().isOk());
should be 应该
mvc.perform(patch("/myPath").contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(locale))).andExpect(status().isOk());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.