我正在尝试从本地计算机上读取Json(文本)文件,并通过Jersey& amp; AngularJs。
Web.xml中
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Your REST end Point</display-name>
<servlet>
<servlet-name>MyRESTEndPoint</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.itc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyRESTEndPoint</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Controller.java
@Path("/")
public class Controller {
@GET
@Path("/receive")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject crunchifyREST(int a) throws FileNotFoundException {
System.out.println("Value received from User is: "+a);
String string = "";
InputStream crunchifyInputStream = new FileInputStream("C:/abc.txt");
InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream);
BufferedReader br = new BufferedReader(crunchifyReader);
String line;
try {
while ((line = br.readLine()) != null) {
string += line + "\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(string);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return jsonObject;
}
}
Controller.js
var app = angular.module('myApp', []);
app.controller('myController',[ '$scope', '$http', function($scope, $http){
$scope.myFunc = function() {
var d=$scope.num;
$http.get('receive',d ).success(function(dataTable)
{
$scope.result = dataTable;
}).error(function(error)
{
console.log(error);
});
}
}]);
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Task</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"> </script>
<script type="text/javascript" src="Controller.js"></script>
</head>
<body>
<div data-ng-app="myApp" data-ng-controller="myController">
<form id="loginForm" class="form-group" >
<input type=number data-ng-model="num">
<button type="submit" class="btn btn-sm btn-success" data-ng-click="myFunc()" >Submit</button>
<h1>{{ result.name }}</h1>
</form>
</div>
</body>
</html>
我已经包含以下罐子 -
asm-all-3.3.1
jersey-core-1.17.1
jersey-server-1.17.1
jersey-servlet-1.17.1
jersey-json-1.17.1
jersey-bundle-1.17.1
json-20090211
从HTML我只是将一个虚拟数字作为参数传递给$ http。但我得到415不支持的媒体类型。在调试模式下,我可以看到Json文本文件已从系统中读取,并且无法发送该响应。 任何帮助?
答案 0 :(得分:0)
首先,@Consumes
注释用于指定发送请求正文的POST请求的application/contentType
标题,因此您在GET请求中并不真正需要它。 / p>
其次,您似乎正在尝试使用Path参数,但是您没有使用正确的语法。
尝试使用您的控制器:
@Path("/")
public class Controller {
@GET
@Path("/receive/{a}")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject crunchifyREST(@PathParam("a") int a) throws FileNotFoundException {
System.out.println("Value received from User is: "+a);
String string = "";
InputStream crunchifyInputStream = new FileInputStream("C:/abc.txt");
InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream);
BufferedReader br = new BufferedReader(crunchifyReader);
String line;
try {
while ((line = br.readLine()) != null) {
string += line + "\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(string);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return jsonObject;
}
}
或者,您可以使用Query参数。
答案 1 :(得分:0)
您需要在请求标头中将Content-Type设置为application / json。您的服务器不了解您的要求?如果有效,请尝试以下。
var app = angular.module('myApp', []);
app.controller('myController',[ '$scope', '$http', function($scope, $http){
$scope.myFunc = function() {
var d=$scope.num;
$http({
url: 'receive',
method: 'GET',
data: d,
headers: {'Content-Type': 'application/json'}
}).then(function (dataTable) {
$scope.result = dataTable;
}).error(function (error) {
console.log(error);
});
}
}]);