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; 
} 
相关问题