需要从spring mvc控制器返回json数据

时间:2012-12-02 20:51:58

标签: ajax json spring java-ee

我正在尝试从spring mvc控制器返回一个地图,进行ajax调用,但我没有得到正确的resposne。

我在配置文件中使用了mvc注释标记,并在我的库中包含了jackson jar文件。

我的要求是将Map返回到我的Ajax调用成功,这样我就可以修改html中的表行。

控制器代码:

@RequestMapping(value="/pricingrecall.do", method=RequestMethod.POST)
    @ResponseBody
    public Map<Integer,String>  pricingUpdate(@RequestParam(value = "opp_Code", required = false) String opp_Code,
            @RequestParam(value = "ref_id", required = false) String ref_id,
            ModelMap model,
            HttpServletRequest request, HttpServletResponse response) throws SQLException, Exception{

            String User="fe0777";
        List<CrossListViewBean>updatedRow = new ArrayList<CrossListViewBean>();
        //String message="";
        logger.info(methodLocation+"|"+"Calling pricing recall ....");
        Map<String, Object> result = new HashMap<String, Object>();
        updatedRow=crossCampService.getupdatedrowListview(opp_Code, ref_id, user);
        Map<Integer,String> lbean= new HashMap<Integer,String>(); 
        lbean=crossCampService.getUpdatedDataPosition(updatedRow.get(0));
        return lbean;

    }

从Ajax调用:

                                    jQuery.ajax( {                                         
                                    url : '/Web/pricingrecall.do',
                type: "POST",
                cache : false,
                timeout : 60000,
                data : {
                    opp_Code :CampId ,
                    ref_id : index
                },
                success : function(result, textStatus, request) {
                    if(result)
                    {   
                        alert(result);
                        //jQuery(".note"+index).html(data);

                    }else
                    {
                        alert("The user session has timed out. Please log back in to the service.");
                        window.location.replace("logout.do");
                    }
                },
                error : function(request, textStatus, errorThrown) {
                    alert("The system has encountered an unexpected error or is currently unavailable. Please contact the support number above if you have any questions.");
                }
            });

这里的ajax成功我总是得到错误,它被转移到错误字符串。 如何在ajax成功中从MAp获得Json

请帮忙

2 个答案:

答案 0 :(得分:0)

我正在使用flexjson来使json输出正确。我使用flexjson附加了我的示例代码。您可以将其用作参考并重新构建控制器方法以输出更正的json。这个link将帮助您了解如何序列化地图。

@RequestMapping(value = "/{id}", headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> findUser(@PathVariable("id") Long id) {
     User user = userService.find(id);

     HttpHeaders headers = new HttpHeaders();
     headers.add("Content-Type", "application/json; charset=utf-8");

     return new ResponseEntity<String>(user.toJson(), headers, HttpStatus.OK); 
}

@Entity
public class AppUser {
    @NotNull
    private String firstName;

    @NotNull
    private String lastName;

    //Getter Setter goes here

    public String AppUser.toJson() {
       return new JSONSerializer().exclude("*.class").serialize(this);
    } 
}

答案 1 :(得分:0)

我在控制器方法上使用了jackson-databind和@ResponseBody注释,它自动将返回的数据转换为json成功。如果您使用maven,请将这些依赖项添加到您的pom.xml(jackson.version对我来说是2.4.0)。

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

否则,您可以将jar文件添加到类路径中。