JSON前缀配置的春季问题 [英] Spring issues with configuration of JSON Prefix

查看:103
本文介绍了JSON前缀配置的春季问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置Spring项目以添加JSON前缀)]}',\n,以克服常见漏洞.我尝试根据每个此链接进行配置,这会在启动服务器时导致异常.请帮我解决这个问题.

I am configuring my Spring project to add JSON Prefix )]}',\n to overcome common vulnerability. I tried configuring per this link, its causing exception while starting up the server. Please help me out resolving 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);
    }
}

我正在得到以下例外,

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

我已经在我的spring-servlet.xml中包含了<mvc:annotation-driven />,我们是否还有其他手动方法可以在较旧版本的Jackson中添加前缀,例如Jackson 1.6?

I have included <mvc:annotation-driven /> in my spring-servlet.xml Do we have any other manual methods to add the prefix in older version's of Jackson, say Jackson 1.6?

更新:

该问题已在Jackson 2.0中解决,并且可以在浏览器中查看前缀,但是无法从角端看到输出.

The problem is fixed with Jackson 2.0 and I'm able to view the prefix in the browser, however I am not able to see the output from angular end.

我的配置就像:

<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>

并且JSON输出是

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

我对这个输出感到困惑,除了Angular无法识别输出并且无法从该对象读取值.任何帮助将是巨大的.提前致谢.

I'm perplexed with this output, besides Angular is not recognizing the output and not able to read the values from this object. Any help would be greatful. Thanks in advance.

推荐答案

我对这个输出感到困惑,除了Angular无法识别 输出并且无法从该对象读取值.任何帮助 会很棒的.

I'm perplexed with this output, besides Angular is not recognizing the output and not able to read the values from this object. Any help would be greatful.

问题与Spring xml配置有关:似乎某些字符被转义了(也许\ n被转义了),因此如果您使用

the issue is related to the Spring xml configuration: it seems that some character are escaped (maybe \n is escaped) so that 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>

前缀是字符串)]}',\ n 而不是)]}'' +新行. 因此,我使用的一种变通方法是创建一个类(即CustomMappingJackson2HttpMessageConverter),该类扩展了原始的org.springframework.http.converter.json.MappingJackson2HttpMessageConverter并覆盖了setJsonPrefix方法,就像这样:

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

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");
    }   

}

我的配置是:

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

通过这种方式,您可以将配置保留在xml文件中,并为需要需要2行的角进行正确的JSON序列化/反序列化,对于)]}}',' 字符,第二个字符用于JSON本身,因此Angular可以完美地工作

in this way you can keep the configuration in the xml file and allow the correct JSON serialization/deserialization for angular that needs 2 lines, the first one for the )]}', characters and the second one for the JSON itself, in this way Angular works perfectly

一行显示JSON响应

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

JSON响应分为两行

JSON response in two lines

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

  • AngularJS版本. 1.5.9
  • Spring MVC版本. 4.2.8
  • 希望这会有所帮助

    这篇关于JSON前缀配置的春季问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆