gpt4 book ai didi

java - 无法配置 Jackson 混音

转载 作者:行者123 更新时间:2023-12-01 18:27:25 26 4
gpt4 key购买 nike

您能否帮助我正确配置JacksonSpring MVC混合注释以自定义JSON响应。

这就是我现在拥有的:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

我不知道如何配置它来映射这两个类...:addMixInAnnotations(User.class, UserMixIn.class);

最佳答案

Controller 构造函数中初始化后,将 mixin 配置添加到 ObjectMapper 中:

@Controller
public class MyController {

private ObjectMapper objectMapper = new ObjectMapper();

public MyController(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.addMixInAnnotations(User.class, UserMixIn.class);
}

@RequestMapping("/some-path")
@ResponseBody
public String someMethod() {
List<User> users = new ArrayList<User>(); // Mock List to hold your Users
users.add(new User()); // Keep adding some users
return objectMapper.writeValueAsString(users, new TypeReference<List<User>>() {});
}
}

并查看输出:)

关于java - 无法配置 Jackson 混音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584183/

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