Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 将spring、hibernate应用程序中的数据公开为restful_Json_Spring_Hibernate - Fatal编程技术网

Json 将spring、hibernate应用程序中的数据公开为restful

Json 将spring、hibernate应用程序中的数据公开为restful,json,spring,hibernate,Json,Spring,Hibernate,这项工作是从这个链接衍生出来的 . 我曾尝试以json格式发布应用程序的数据,并在controller类中对其进行了进一步修改 @Controller @RequestMapping(value="/team") public class TeamController { @Autowired private TeamService teamService; @RequestMapping(value="/add", method=RequestMethod.GET) public Mode

这项工作是从这个链接衍生出来的 . 我曾尝试以json格式发布应用程序的数据,并在controller类中对其进行了进一步修改

@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格式发布数据?

将json代码更改为以下内容:

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

        return teamService.getTeams();
    }

}
注意:TEAMS\u OBJECT\u类型需要替换为teamService.getTeams返回的类型


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

您能否向我们展示调用/team/api/teamreplaced TEAMS\u对象类型但不起作用的代码。我正在尝试从这个示例中学习。是否可以以相同的方式实现此应用程序?teamService.getTeams返回什么?另外,外接程序显示从何处调用/team/api/team的代码