简体   繁体   English

AngularJS调用Java中的Restful Webservice返回错误

[英]AngularJS call to Restful Webservice in java returning an error

I have a restful webservice that returns a list of users .I am calling the method using AngularJS My Restful Webservice: 我有一个RESTful Web服务,它返回用户列表。我正在使用AngularJS调用该方法。

package webservice;
import java.sql.Connection;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import model.ProjectManager;
import com.google.gson.Gson;
import dto.Users;
@Path("/WebService")
public class UsersService {
@GET
@Path("/GetUsers")
@Produces("application/json")
public String user()
{
  String users = null;
  try 
  {
    ArrayList<Users> usersList = null;
    ProjectManager projectManager= new ProjectManager();
    usersList = projectManager.GetUsers();
    Gson gson = new Gson();
    System.out.println(gson.toJson(usersList));
    users = gson.toJson(usersList);
  }
  catch (Exception e)
  {
    System.out.println("Exception Error"); //Console 
  }
   return users;
 }
}

and the output is: 输出为:

[{"userId":1,"userName":"sdsouza"},{"userId":3,"userName":"administrator"}]

The http url is yielding the results just fine. http url产生的结果很好。

My controllers.js in Angular Js: 我在Angular Js中的controllers.js:

angular.module('starter.controllers', [])
.controller('DashCtrl', ['$scope', '$http', function($scope, $http) {
$http.get( 
'http://localhost:8081/TestDataRestful/REST/WebService/GetUsers'
).success(function(data) {          
    $scope.$apply(function () {
     $scope.users = data;
     });
      })
   .error(function(data) {
    $scope.message="error";
    });
  }])

When I run the html page,I see that the call is getting made.The console prints out the result but the html page prints out the error message and there is no sight of the users.I am a noob.I would be grateful if anyone can spot the error.I have been battling with it for quite some time.Thanks in advance. 当我运行html页面时,我看到调用已完成。控制台会打印出结果,但html页面会打印出错误消息并且看不到用户。我是菜鸟。任何人都可以发现该错误。我已经为之奋斗了一段时间。

 <ion-view view-title="Dashboard">
 <ion-content class="padding">
 <div class="list card">
 <div class="item item-divider">Recent Updates</div>
 <div class="item item-body">
 {{message}}
 </div>
 <div class="item item-body" ng-repeat="user in users">
 {{user}}
 </div>
 </div>
 </ion-content>
 </ion-view>

It was not the code.I needed to add CORS filter in the webapp's web.xml file. 这不是代码。我需要在webapp的web.xml文件中添加CORS过滤器。 If you are using Tomcat 7 and above,there is no need to add cors jars,just include the filters in the xml file. 如果您使用的是Tomcat 7及更高版本,则无需添加cors jar,只需在xml文件中包含过滤器即可。

http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter

<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-  
Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-
Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Special thanks to Mike's article.I used ionic.So I could skip the first configuration of including the httpProvider in the js file. 特别感谢Mike的文章。我使用了ionic。因此,我可以跳过在js文件中包含httpProvider的第一个配置。

http://mikesknowledgebase.azurewebsites.net/pages/Services/WebServices-Page8.5.htm http://mikesknowledgebase.azurewebsites.net/pages/Services/WebServices-Page8.5.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM