春天JSON导致当Spring框架升级,从3.0.2到3.2.0的问题 [英] Spring JSON causes problems when the Spring framework is upgraded from 3.0.2 to 3.2.0

查看:169
本文介绍了春天JSON导致当Spring框架升级,从3.0.2到3.2.0的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Web应用程序使用Spring框架3.0.2版随Hibernate(的NetBeans 6.9.1)工作。后来我才知道,那里是这是造成在上传多个文件问题的一个bug正如我在的$一个p $ pvious 问题

I was working with a web application using the Spring framework version 3.0.2 along with Hibernate (NetBeans 6.9.1). Later I came to know that there was one of bugs that was causing problems in uploading multiple files as mentioned in my one of previous questions.

我已经完成了艰难地获得了解决方案,但未能成功。因此,我升级Spring版本来 3.2.0

I have finished struggling to acquire the solution but couldn't succeed. Therefore, I upgraded the Spring version to 3.2.0.

使用以前的版本(3.0.2),AJAX是工作的罚款用的杰克逊1.9.8 (其下载 页面),但后来版本(3.2.0),一切工作正常,但AJAX中的JavaScript的code呼吁警惕错误随处可见。

With the earlier version (3.0.2), AJAX was working fine with Jackson 1.9.8 (its download page) but with the later version (3.2.0), everything works fine but AJAX calls alert an error everywhere in the JavaScript code.

有一个场景在一个地方,相应的状态列表是从春节控制器中检索与DAO。被映射与在Spring控制器的URL方法如下,

There is a scenario at one place when one of the countries is selected in the country select box, the corresponding state list is retrieved from the Spring controller along with DAO. The method which is mapped with a URL in the Spring controller is as follows,

@RequestMapping(value="ajax/GetStateList", method=RequestMethod.GET)
public @ResponseBody List<Object[]> getStateSelectBox(HttpServletRequest request)
{
    return cityService.getStateSelectBox(request.getParameter("countryId"));
}   

在全国选择框中选择一个国家调用此方法。该 getStateSelectBox()方法是在DAO类的一个定义如下,

This method is invoked when a country is selected in the country select box. The getStateSelectBox() method is defined in one of DAO classes as follows,

@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)
public final class CityDAO implements CityService
{
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    @SuppressWarnings("unchecked")
    public List<Object[]> getStateSelectBox(String id)
    {
        List<Object[]> list = sessionFactory.getCurrentSession()
                          .createQuery("select s.stateId, s.stateName from StateTable s where countryId.countryId=:id order by s.stateId")
                          .setParameter("id", Long.parseLong(id)).list();

       for(Object[]o:list)
       {
           System.out.println(o[0]+" : "+o[1]);
       }
       return list;
   }
}

的foreach 循环只是为了演示,它会显示所有的状态以及他们的ID对应 countryId 的AJAX请求,但列表提供的是不返回到JSP。

The foreach loop is just for the sake of demonstration, it displays all the states along with their id that correspond to the countryId supplied by the AJAX request but the List is not returned to JSP.

用于发送此Ajax请求的Jav​​aScript的code警报错误。它似乎有一些问题JSON映射。同样的事情正与Spring框架(3.0.2)的早期版本。我不知道为什么会出现这种问题的原因与Spring的更高版本,这是3.2.0。是否有与Spring 3.2.0版什么,我可能会丢失?

The JavaScript code used to send this AJAX request alerts an error. It appears that there are some problems with JSON mapping. The same thing was working with the earlier version of the Spring framework (3.0.2). I'm not sure why does this cause problems with the higher version of Spring which is 3.2.0. Is there anything with the Spring version 3.2.0 which I might be missing?

如果你需要看到JavaScript的code,完整的JavaScript code来实现,这将是如下:

If you needed to see the JavaScript code, the full JavaScript code to achieve this would be as follows.

function getStates(countryId)
{
    if(countryId==""||countryId==null||countryId==undefined||isNaN(countryId))
    {
        var str="<select id='cmbStates' name='cmbStates' onchange='errorMessage(this.value);' class='validate[required] text-input'><option value=''>Select</option></select>";
        $('#stateList').html(str);
        alert("Please select an appropriate option.");
        return;
    }

    var div=document.createElement("div");
    div.id="temp";
    document.body.appendChild(div);

    $.ajax({
        datatype:"json",
        type: "GET",
        contentType: "application/json",
        url: "/wagafashion/ajax/GetStateList.htm",
        data: "countryId=" + countryId+"&t="+new Date().getTime(),
        success: function(response)
        {
            if(typeof response==='object'&&response instanceof Array)
            {                            
                var str="<select id='cmbState' name='cmbState' onchange='errorMessage(this.value);' class='validate[required] text-input'><option value=''>Select</option>";
                var l=response.length;

                for(var i=0;i<l;i++)
                {
                    str+="<option value='"+response[i][0]+"'>"+$('#temp').text(response[i][1]).html()+"</option>";
                }
                str+="</select>";
                $('#stateList').html(str); // select box is written to #stateList div
                $('#temp').remove();
            }
        },
        error: function(e)
        {
            alert('Error: ' + e);
        }
    });
}            


当然,杰克逊库在classpath和我没有收到在服务器端的任何错误或异常。 AJAX请求成功,并且它会通过Spring中的DAO和类型的列表名单,其中,对象[] GT; 从数据库中检索,但它不是JSON的响应到的JSP(可能/应映射到JavaScript数组)。 presumably,似乎有什么用JSON映射缺少这是但不符合春天的早期版本的情况。


To be sure, the Jackson library is on the classpath and I'm not getting any error or exception on the server side. The AJAX request succeeds and it goes to the DAO via Spring and the list of type List<Object[]> is retrieved from the database but it is not a response of JSON to JSP (which could/should be mapped to a JavaScript array). presumably, it appears that there is something missing with JSON mapping which was however not the case with the earlier version of Spring.

编辑:

我试图解析名单,其中,对象[] GT; 在两个框架,3.0.2和3.2.0,如

I have tried to parse List<Object[]> in both of the frameworks, 3.0.2 and 3.2.0 such as

List<Object[]> list = cityService.getStateSelectBox(request.getParameter("countryId"));
ObjectMapper objectMapper=new ObjectMapper();
try
{
    objectMapper.writeValue(new File("E:/Project/SpringHibernet/wagafashionLatest/temp.json"), list);
}
catch (IOException ex){}

文件 temp.json 包含以下字符串。

[[21,"Gujarat"],[22,"Maharashtra"],[23,"Kerala"],[24,"New Delhi"]]

在这两种情况下(与两个框架)。因此,看来JSON响应应该是在这两种情况下相同。

In both the cases (with both frameworks). So, it appears that the JSON response should be same in both the cases.

如下 temp.json 文件也可以反序列化。

The temp.json file can also be deserialized as follows.

try
{
    ObjectMapper mapper=new ObjectMapper();
    List<Object[]> list = mapper.readValue(new File("E:/Project/SpringHibernet/wagafashionLatest/temp.json"), new TypeReference<List<Object[]>>() {});

    for(Object[]o:list)
    {
        System.out.println(o[0]+" : "+o[1]);
    }
} 
catch (IOException ex) 
{

}

它工作正常和的foreach 循环遍历列表类型名单,其中, [对象]&GT; 。因此,问题可能由Spring框架本身引起的。还有什么是必需的,我不知道。为什么它没有被映射杰克逊?

It works fine and the foreach loop iterates over the List of type List<Object[]>. So, the problem might be caused by the Spring framework itself. What else is required, I'm not sure. Why is it not mapped by Jackson?

推荐答案

我使用Spring 3.1.3和我发现,杰克逊映射尝试创建在响应根对象。这是与Jackson2映射器。我没有尝试过与旧杰克逊映射器。如果你也升级 - 杰克逊,你可能会看到同样的问题。

I'm using Spring 3.1.3 and I've found that the Jackson mapping attempts to create a root object in the response. This is with the Jackson2 mapper. I haven't tried with the older Jackson mapper. If you also upgraded Jackson you may be seeing the same issue.

在过去,一个对象数组将被映射像

In the past, an object array would be mapped something like

[{name:'name1',id:4},{name:'name2',id:6}]

现在我发现,他们正在为对象提供自动生成的对象名称,所以它返回类似

Now I find that they are providing an auto-generated object name for the object, so it is returned something like

{ objectArray: [{name:'name1',id:4},{name:'name2',id:6}]}

所以,你需要引用 response.objectArray [0] ,而不是能够直接引用响应[0]

So you need to reference response.objectArray[0] instead of being able to directly reference response[0]

在任何情况下,这是可能的,JSON响应已经有所改变格式。你应该看看新的反应,看看什么样的变化需要发生在JavaScript中,使其映射新的结构。

In any case, it's probable that the JSON response has changed format somewhat. You should look at the new response and see what changes need to occur in the javascript to make it map the new structure.

这篇关于春天JSON导致当Spring框架升级,从3.0.2到3.2.0的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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