好的,我已经完成了。
因此,要做到这一点的方法是使用注入的Jackson2ObjectMapper手动序列化响应 :)
让我们看一个简单的主应用程序类:
@SpringBootApplication
public class StackoverflowApplication {
public static void main(String[] args) {
SpringApplication.run(StackoverflowApplication.class, args);
}
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true);
return builder;
}
}
一个REST控制器类:
@RestController
public class TestRestController {
private static final Logger LOGGER = Logger.getLogger(TestRestController.class);
@Autowired
Jackson2ObjectMapperBuilder objectMapperBuilder;
@RequestMapping(value = "/testendpoint/{somevalue}", method = RequestMethod.GET, produces="application/json")
public @ResponseBody String customersLastVisit(@PathVariable(value = "somevalue") Integer number,
@RequestParam(value = "pretify", defaultValue = "false") Boolean pretify) throws JsonProcessingException {
if (pretify) {
LOGGER.info("Pretify response!");
objectMapperBuilder.indentOutput(true);
}
else {
objectMapperBuilder.indentOutput(false);
}
ObjectMapper mapper = objectMapperBuilder.build();
String jsonResponse = mapper.writeValueAsString(new TestDTO());
return jsonResponse;
}
}
DTO类:
public class TestDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public TestDTO() {
super();
id = new Integer(1);
name = new String("SomeName");
}
}
http://localhost:8080/testendpoint/1 返回 {"id":1,"name":"SomeName"}
而http://localhost:8080/testendpoint/1?pretify=true 返回
{
"id" : 1,
"name" : "SomeName"
}
编辑:
如果您想在每个控制器中使用它,请按照以下方式操作:
public class PretifyingInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOGGER = Logger.getLogger(PretifyingInterceptor.class);
@Autowired
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String pretify = request.getParameter("pretify");
Boolean isPretify = Boolean.parseBoolean(pretify);
LOGGER.info("Should be pretified: " + isPretify);
if (isPretify) {
mappingJackson2HttpMessageConverter.setPrettyPrint(true);
}
else {
mappingJackson2HttpMessageConverter.setPrettyPrint(false);
}
return true;
}
}
现在添加新的拦截器:
@Configuration
public class InterceptorConfigurerAdapter extends WebMvcConfigurerAdapter{
@Bean
PretifyingInterceptor pretifyingInterceptor() {
return new PretifyingInterceptor();
}
@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(pretifyingInterceptor())
.addPathPatterns("/**");
}
}
现在控制器可以看起来像这样:
@RequestMapping(value = "/testendpoint/{somevalue}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody TestDTO customersLastVisit(@PathVariable(value = "somevalue") Integer number,
@RequestParam(value = "pretify", defaultValue = "false") Boolean pretify) throws JsonProcessingException {
return new TestDTO();
}
编辑2: 为了避免拦截器之间共享状态,将拦截器Bean的作用域定义为
请求(request)
。
希望这有所帮助 :)
Jackson2ObjectMapper
,然后根据请求调用其方法indentOutput(boolean)
。 我相信这个主题可能会有所帮助:https://dev59.com/Ql4c5IYBdhLWcg3wFm84 - patrykos91