我试图在 Spring MVC 中将一些对象序列化为 JSON。我使用这种方法:
@Controller
@RequestMapping(produces = "application/json")
public class SurveyAPIController {
SurveyService surveyService;
@Autowired
public SurveyAPIController(SurveyService surveyService) {
this.surveyService = surveyService;
}
//*****************************
// GET
//*****************************
@RequestMapping(value = "/survey/{id}",produces = "application/json;charset=UTF-8")
@ResponseBody
public Survey getSurvey(@PathVariable("id") int id) {
return surveyService.getSurvey(id);
}
}
Survey 对象中有 Set 字段。如果该 Set 为空,则无法创建 JSON,并且出现此错误:
HTTP 500 - The server encountered an internal error that prevented it from fulfilling this request.
我该如何解决这个问题,?