Spring JSON prefix setup problems

I am configuring my Spring project to add JSON prefix )]}',\n

to overcome a common vulnerability. I tried to configure for this link , which throws an exception when starting the server. Please help me to solve this.

@Configuration
@EnableWebMvc
public Class WebAppConfig extends WebMvcConfigurationSupport
{
    public void addPrefixJSON()
    {
        List<HttpMessageConverter<?>> converters = super.getMessageConverters();
        MappingJackson2HttpMessageConverter convert = new MappingJackson2HttpMessageConverter();
        convert.setPrefixJson(true);
        converters.add(convert);
    }
}

      

and i am getting follwing exception,

08:01:23,435 ERROR [org.springframework.web.context.ContextLoader] 
(ServerService Thread Pool -- 68) Context initialization failed: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource 
[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is 
org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping()] threw exception; nested exception is 
java.lang.ClassCastException: org.springframework.web.accept.ContentNegotiationManagerFactoryBean$$EnhancerByCGLIB$$6af53d42 cannot be cast to 
org.springframework.web.accept.ContentNegotiationManager

      

I have included <mvc:annotation-driven />

in my spring-servlet.xml. Do we have any other manual methods for adding a prefix to an older version of Jackson, say Jackson 1.6?

Update:

The issue was fixed with Jackson 2.0 and I can view the prefix in the browser, however I cannot see the result from the angular end.

My config is like:

<mvc:annotation-driven content-negotiation-manager="contentManager">
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="jsonPrefix" value=")]}',\n" />
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

      

and the JSON output is

)]}',\n"{\"userName\":\"ABC\",\"emailId\":\"ABC@gmail.com\"}"

      

I am puzzled by this output, furthermore angular does not recognize the output and cannot read values ​​from this object. Any help would be helpful. Thank you in advance.

0


source to share


3 answers


I am puzzled by this output, furthermore Angular does not recognize the output and cannot read the values ​​of this object. Any help would be great.

the problem is with the Spring xml configuration: it seems that some character is escaped (possibly \ n escaped), so if you use

<mvc:annotation-driven content-negotiation-manager="contentManager">
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="jsonPrefix" value=")]}',\n" />
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

      

the prefix is ​​a string)]} ', \ n instead of)]}', + newline. So the workaround I used was to create a class (like CustomMappingJackson2HttpMessageConverter) that extends the original org.springframework.http.converter.json.MappingJackson2HttpMessageConverter method and overrides the setJsonPrefix method like this:

public class CustomMappingJackson2HttpMessageConverter
        extends org.springframework.http.converter.json.MappingJackson2HttpMessageConverter {

    public CustomMappingJackson2HttpMessageConverter() {
        super();
    }

    public CustomMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @Override
    public void setJsonPrefix(String jsonPrefix) {
        super.setJsonPrefix(jsonPrefix+"\n");
    }   

}

      

and my config:

<mvc:annotation-driven>
        <mvc:message-converters>
             <bean class="mypackage.CustomMappingJackson2HttpMessageConverter">
                 <property name="jsonPrefix" value=")]}'," />
             </bean>
         </mvc:message-converters>
    </mvc:annotation-driven>

      



this way you can save the configuration in xml file and allow proper JSON serialization / deserialization for Angular which needs 2 lines , the first is for )]} ' , and the second is for the JSON itself, this way Angular works great

JSON response in one line

1 )]}',\n["id":1,"name":"Marco"....]

      

JSON response in two lines

1 )]}',
2 ["id":1,"name":"Marco"....]

      

  • AngularJS ver. 1.5.9
  • Spring MVC ver. 4.2.8

hope this helps

+1


source


In Spring Lemon, simply configuring the bean as shown below instead of the above configuration:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setJsonPrefix(")]}',\n");

    return converter;
}

      



An easy way to check if it will work is to look at the response data, for example. on the Network tab in chrome.

+1


source


You can XML-encode a newline character to allow customization exclusively in XML.

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="jsonPrefix" value=")]}',&#10;" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

      

+1


source







All Articles