Search in sources :

Example 81 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project topcom-cloud by 545314690.

the class MyWebAppConfigurer method extendMessageConverters.

/**
 * 添加返回结果缩进支持,如果存在pretty参数,则返回结果添加缩进
 * @param converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.replaceAll(c -> {
        if (c instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper) {

                @Override
                protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
                    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
                    if (attributes != null && attributes instanceof ServletRequestAttributes) {
                        String attribute = ((ServletRequestAttributes) attributes).getRequest().getParameter("pretty");
                        if (attribute != null) {
                            generator.setPrettyPrinter(new DefaultPrettyPrinter());
                        }
                    }
                    super.writePrefix(generator, object);
                }
            };
            return converter;
        } else {
            return c;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)81 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2