Spring Controller不将JSON数据返回到有角度的http GET调用

时间:2018-12-14 08:27:05

标签: java angularjs json spring spring-mvc

我有一个看起来像这样的弹簧控制器:

@RestController
public class RaceResultController {

    @Autowired
    RaceResultImpl raceResultImpl;

    @GetMapping("/race")
    public ModelAndView hello(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        List<RaceTopResult> raceTopResultList = raceResultImpl.getResults();
        ObjectMapper o = new ObjectMapper();
        model.addAttribute("races", raceTopResultList);
        return new ModelAndView("race");


    }
}

然后,我在race.html视图中嵌入了一些角度代码:

      <head>
        <title>F1 RESULTS 2018</title>
         <script 


      src=
    "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> .    
     </script>
      <script 
       src=
    "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
     </head>
     <body>
     <header>
        <h1>F1 RESULTS 2018</h1>
     </header>
      <div ng-app="raceApp" ng-controller="raceCtrl">
        <table id="my_table" border = "1">
            <tr ng-repeat="race in races">
                <td>{{ race.driver }}</td>
                <td>{{ race.team }}</td>
            </tr>
        </table>
     </div>

    </body>

    <script>
        var app = angular.module('raceApp', []);
        app.controller('raceCtrl', function($scope, $http) {
            alert("mini");
            $http({
                url: "/race",
                method: "GET",
                dataType: 'json'
                 }).then(function successCallback(response) {
                alert("hi")
                 $scope.races = response.data;
                 }, function errorCallback(response) {
                 alert("bye");
                 $scope.error = response.statusText;
                 });
                   });
                 </script>

当我在浏览器中点击/race网址时,即使我单独测试控制器,它也总是转到错误回调块。我可以看到它从服务返回了数据,但是我无法在有角度的http响应中获取数据。请帮忙。

谢谢

2 个答案:

答案 0 :(得分:1)

如果您想找回json响应,请执行此操作

@GetMapping("/race")

    public List<RaceTopResult> hello(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        List<RaceTopResult> raceTopResultList = raceResultImpl.getResults();
        //ObjectMapper o = new ObjectMapper();
        //model.addAttribute("races", raceTopResultList);
        return raceTopResultList;


    }

如果在类路径中具有杰克逊依赖关系,则您的上述结果将自动转换为json,如果遇到任何错误,请在maven依赖关系中查找杰克逊

如果您想返回带有模型数据的html视图,请使用

@Controller //use controller not RestController
public class RaceResultController {

    @Autowired
    RaceResultImpl raceResultImpl;

    @GetMapping("/race")
    public ModelAndView hello(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        List<RaceTopResult> raceTopResultList = raceResultImpl.getResults();
       // ObjectMapper o = new ObjectMapper();
        model.addAttribute("races", raceTopResultList);
         return new ModelAndView(model,"race");


    }
}

答案 1 :(得分:0)

如果您使用的是@RestController,请不要使用ModelAndView作为任何方法的返回类型。
保持您的对象为返回类型,spring会将其转换为JSON响应。

ObjectMapper是不需要的,因为我们不是手动完成的,Spring会为我们做的。

因此,您的控制器应如下所示:

@RestController
public class RaceResultController {

    @Autowired
    RaceResultImpl raceResultImpl;

    @GetMapping("/race")
    public List<RaceTopResult>hello(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        List<RaceTopResult> raceTopResultList = raceResultImpl.getResults();

        return raceTopResultList;


    }
}