Ajax 需要从SpringMVC控制器返回json数据

Ajax 需要从SpringMVC控制器返回json数据,ajax,json,spring,jakarta-ee,Ajax,Json,Spring,Jakarta Ee,我试图从SpringMVC控制器返回一个映射,进行ajax调用,但没有得到正确的响应 我在配置文件中使用了mvc注释标记,并在我的库中包含了jackson jar文件 我的要求是将Map返回到Ajax调用的success,这样我就可以在html中修改表行 控制器的代码: @RequestMapping(value="/pricingrecall.do", method=RequestMethod.POST) @ResponseBody public Map<Integer,

我试图从SpringMVC控制器返回一个映射,进行ajax调用,但没有得到正确的响应

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

我的要求是将Map返回到Ajax调用的success,这样我就可以在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成功的过程中,我总是遇到错误,它被转移到错误字符串中。 如何在ajax中成功地从映射中获取Json


请帮助

我正在使用flexjson来获得正确的json输出。我附加了我的一个示例代码,使用flexjson。您可以将其用作参考,并重新构造控制器方法以输出correcct json。这将帮助您序列化映射

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

我使用jackson databind并在控制器方法上使用@ResponseBody注释,它自动将返回的数据成功地转换为json。如果您使用maven,请将这些依赖项添加到pom.xml中。我的版本是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文件添加到类路径。

显示一些代码。换句话说,我更新了我的问题:需求是我需要来自控制器的json响应:它应该是{1:Denver,2:texas}etcdid您声明json对象映射器?我需要在哪里声明json对象映射器
<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>