0

我有一個Spring 3控制器,它返回一個JSON對象。我使用@ResponseBody註釋和jackson-mapper-asl jar,Spring會使用它自動處理JSON轉換。 3個返回語句返回不同的JSON格式。這可以通過用Object修改getPersonDetails方法的返回類型來處理,還是有更好的方法。Spring @ResponseBody JSON

@RequestMapping(value="/test", method=RequestMethod.GET) 
public @ResponseBody List<Person> getPersonDetails() { 


    List<Person> listPerson = null; 
    try { 
     // Call to service and get the list of Person 
     listPerson = getPersonList(); 
     if(CollectionUtils.isNotEmpty(listPerson)) { 
      // Return JSON object 
      //{"Name":"XYZ", "Age":25} 
     } else { 
      // Return JSON object 
      //{"InformationMessage":"No data found."} 
     } 
    } catch(final Exception e) { 
     // Return JSON object 
     // {"ExceptionMessage":"Exception in controller."} 
    } 
} 

回答

1

,你能滿足這樣的事情

@RequestMapping(value="/test", method=RequestMethod.GET) 
public @ResponseBody ResponseEntity<List<Person>> getPersonDetails() { 
try{ 
if(dataAvailable) // if success 
{ 

return new ResponseEntity<List<Person>>(yourlist, responseHeaders, HttpStatus.OK); 
} 
else{ 
return new ResponseEntity<List<Person>>(HttpStatus.NOT_FOUND); 
} 

}catch(Exception e){ 
// your custom exception here 
throw new PersonNotFoundException(); 
} 

} 

注:此處鍵入沒有IDE,任何語法錯誤,隨意編輯。

1

我會用ResponseEntity<?>

@RequestMapping(value="/test", method=RequestMethod.GET) 
public @ResponseBody ResponseEntity<List<Person>> getPersonDetails() { 


    List<Person> listPerson = null; 
    try { 
     // Call to service and get the list of Person 
     listPerson = getPersonList(); 
     if(CollectionUtils.isNotEmpty(listPerson)) { 
      return new ResponseEntity<>(listPerson, HttpStatus.OK); 
     } else { 
      return new ResponseEntity(HttpStatus.NOT_FOUND); 
     } 
    } catch(final Exception e) { 
     return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); 
    } 
} 
0

此外:沒有必要通過「信息對象」來表示,出事了。這就是HTTP狀態碼的用途。

我會去注射HttpServletResponse並使用setStatus()來指示錯誤,指示404500。此外,您可以通過getWriter().write("{\"ExceptionMessage\":\"Exception in controller.\"}")

@RequestMapping(value="/test", method=RequestMethod.GET) 
public @ResponseBody List<Person> getPersonDetails(HttpServletResponse response) { 
    List<Person> listPerson = null; 
    try { 
     listPerson = getPersonList(); 
     if(!CollectionUtils.isEmpty(listPerson)) { 
      response.setStatus(HttpServletResponse.SC_NOT_FOUND); 
      response.getWriter().write("Whatever you want"); 
     } 
    } catch(final Exception e) { 
     response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
     response.getWriter().write("Whatever you want"); 
    } 
    return listPerson; 
} 
0

從控制器方法中拋出特定異常。例如,就你的情況而言,如果列表爲空,則自定義PersonNotFoundException,對於未知錯誤,則爲ServiceException。並通過返回類型爲ResponseEntity的@ExceptionHandlers處理這些異常。

控制器方法應該只有返回類型爲List。

這將幫助你有一個整潔的文檔。

嘗試在春季搜索如何使用ExceptionHandlers。

希望這回答你的問題。

0

您可以創建JResponse類並保存數據。

public class JSonResponse{ 
    private Object result; 
    private String statu; 

    // 
} 


@RequestMapping(value="/test", method=RequestMethod.GET) 
public @ResponseBody JSonResponse getPersonDetails() { 
    JSonResponseres = new JSonResponse(); 
    List<Person> listPerson = null; 
    try { 
    // Call to service and get the list of Person 
    listPerson = getPersonList(); 
    if(CollectionUtils.isNotEmpty(listPerson)) { 
     res.setResult(listPerson); 
     res.status("SUCCESS"); 
    } else { 
     res.setResult("Not Found Data"); 
     res.status("FAIL"); 
    } 
    } catch(final Exception e) { 
    // Return JSON object 
    // {"ExceptionMessage":"Exception in controller."} 
} 

    return res; 
} 
相關問題