使用Java从服务器端的URL中提取参数

时间:2018-09-03 09:26:40

标签: java angularjs ajax rest jax-rs

我正在构建一个在客户端使用AngularJS在服务器端使用Java的应用程序。在主页上,我有几个餐馆类别的链接,这些链接都在单击时导致部分restorani.html。这样我就知道单击了哪个餐厅类别,我在URL中添加了一个ID,因此看起来像这样:

  <a href="#/restorani/1">Chinese food</a> <br />
  <a href = "#/restorani/2">Italian food</a>

Restorani.html需要显示点击类别中的餐馆。 这是我的控制器的外观:

app.controller('RestoranController', function($routeParams, $scope, 
 RestoranService){

function init(){
    RestoranService.getById($routeParams.id).then(function(response){
        $scope.jela = response.data;
    });
}
init();

});

这是我发送get请求的服务:

app.factory('RestoranService', function($http){
var factory = {};

factory.getById = function(id){
    return $http.get('/WebProjekat/rest/restorani/getById/' + id);
}

return factory;

}); 这是我的服务器端代码:

@GET
@Path("/getById/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ArrayList<Artikli> getById(@PathParam("id") String id){
    int idNum = Integer.parseInt(id);
    ArrayList<Restoran> listaRestorana = 
  (ArrayList<Restoran>)ctx.getAttribute("restorani");
    if(listaRestorana == null) {
        listaRestorana = readAllRestorane();
        ctx.setAttribute("restorani", listaRestorana);
    }

    Restoran r = getRestoranById(listaRestorana, idNum);

    return r.getListaJela();
}

对于某些未知原因,我一直收到此错误:

获取http://localhost:8008/WebProjekat/rest/restorani/getById/1 500(内部    服务器错误)。

如果有人告诉我错误在哪里,我将非常感激。很抱歉,如果这是一些琐碎的错误,但是我找不到,并且我已经花了很多时间在上面。

1 个答案:

答案 0 :(得分:1)

由于此错误是'500(内部服务器错误)'。您的应用程序的getById()方法中可能具有Exception。
要调试此方法:-在getById()中添加调试点,并以调试模式运行服务器。并在服务器控制台中查找错误或stackTrace。