暴露来自spring,hibernate应用程序的数据为restful

时间:2014-11-06 23:30:40

标签: json spring hibernate

这项工作来自这个链接 http://www.javacodegeeks.com/2013/04/spring-mvc-hibernate-maven-crud-operations-example.html。我试过尝试以json格式发布应用程序的数据,我已经在控制器类中做了进一步的修改

@Controller
@RequestMapping(value="/team")
public class TeamController {

@Autowired
private TeamService teamService;

@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addTeamPage() {
    ModelAndView modelAndView = new ModelAndView("add-team-form");
    modelAndView.addObject("team", new Team());
    return modelAndView;
}

@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView addingTeam(@ModelAttribute Team team) {

    ModelAndView modelAndView = new ModelAndView("home");
    teamService.addTeam(team);

    String message = "Team was successfully added.";
    modelAndView.addObject("message", message);

    return modelAndView;
}

@RequestMapping(value="/list")
public ModelAndView listOfTeams() {
    ModelAndView modelAndView = new ModelAndView("list-of-teams");

    List<Team> teams = teamService.getTeams();
    modelAndView.addObject("teams", teams);

    return modelAndView;
}

@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public ModelAndView editTeamPage(@PathVariable Integer id) {
    ModelAndView modelAndView = new ModelAndView("edit-team-form");
    Team team = teamService.getTeam(id);
    modelAndView.addObject("team",team);
    return modelAndView;
}

@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public ModelAndView edditingTeam(@ModelAttribute Team team, @PathVariable Integer id) {
    ModelAndView modelAndView = new ModelAndView("home");
    teamService.updateTeam(team);
    String message = "Team was successfully edited.";
    modelAndView.addObject("message", message);
    return modelAndView;
}

@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public ModelAndView deleteTeam(@PathVariable Integer id) {
    ModelAndView modelAndView = new ModelAndView("home");
    teamService.deleteTeam(id);
    String message = "Team was successfully deleted.";
    modelAndView.addObject("message", message);
    return modelAndView;
}

@RequestMapping(value = "/api/team", method = RequestMethod.GET)
//    public @ResponseBody String listUsersJson (ModelMap model) throws JSONException {
    public @ResponseBody
    String listTeamJson () throws JSONException {
        JSONArray userArray = new JSONArray();
        for (Team team : teamService.getTeams()) {
            JSONObject userJSON = new JSONObject();
            userJSON.put("id", team.getId());
            userJSON.put("Name", team.getName());
            userJSON.put("Rating", team.getRating());
            userArray.put(userJSON);
        }
        return userArray.toString();
    }

}

但是此代码显示错误404.如何实现我的应用程序以便以json格式发布数据?

1 个答案:

答案 0 :(得分:0)

将您的JSON代码更改为以下内容:

@RequestMapping(value = "/api/team", method = RequestMethod.GET)
    public @ResponseBody List<TEAMS_OBJECT_TYPE>listTeamJson () throws JSONException {

        return teamService.getTeams();
    }

}

注意:TEAMS_OBJECT_TYPE需要替换为teamService.getTeams()返回的类型。

@ResponseBody会自动将其转换为JSON,因此您需要做的就是在客户端正确阅读。