目标是使控制器返回的Jackson格式响应具有PrettyPrint效果。以下是我的配置:
@EnableWebMvc
@Configuration
public class JacksonConfig extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
}
}
}
这是控制器的外观:
@RequestMapping(value = "/facebook", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Map<String, Object> authorizeViaFacebook(
@RequestParam(value = "token") String token) throws DefaultException{
Facebook facebook = this.facebookUtility.getFacebook(token);
org.springframework.social.facebook.api.User facebookUserProfile = facebook.userOperations().getUserProfile("me");
User loggedInUser = User.signInWithFacebookProfile(facebookUserProfile);
return ImmutableMap.of("token", loggedInUser.tokenForAuthentication(), "user", loggedInUser);
}
但无论我做什么,它仍然是直接打印出来的。我尝试了不同的配置,但仍然没有成功。
这是一个Jackson的POM文件:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
Spring版本为4.2.4.RELEASE。
我该如何使Jackson使用PrettyPrint格式?