2012-12-02 59 views
1

我試圖從spring mvc控制器返回一個Map來做ajax調用,但我沒有得到正確的答案。需要從spring mvc控制器返回json數據

我在我的配置文件中使用了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; 

    } 

電話:

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

在這裏,在阿賈克斯sucess我總是得到錯誤,它就會改行托特他的錯誤字符串。 我怎樣才能從地圖將JSON在阿賈克斯sucess

請幫

+0

顯示一些代碼。換句話說http://whathaveyoutried.com/ –

+0

已更新我的問題:要求是我需要控制器的json resposne:它應該是{1:Denver,2:texas}等 – user1457686

+0

您是否聲明瞭json對象映射器? – mfirry

回答

0

我使用flexjson獲得JSON輸出正確的。我附上我的示例代碼,使用flexjson。您可以將其用作參考,並重構您的控制器方法以輸出correcct 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); 
    } 
} 
0

我用傑克遜-數據綁定,並與控制器方法註釋@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文件添加到您的類路徑中。

相關問題