【发布时间】:2015-08-29 14:03:41
【问题描述】:
我有一个错误,我很困惑,我使用 Jersey 创建了一个简单的 Java EE 7 项目。
我将在我的休息服务中返回这门课:
@XmlRootElement
public class LocationDTOx {
private Long id;
private String tittle;
private String description;
private Long parent;
//Getter and setters...
在我的服务类中,我有:
@Path("/location")
public class LocationService {
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/findlocation")
public LocationDTOx findLocation() {
System.out.println("findlocation");
try {
LocationDTOx x = new LocationDTOx();
x.setDescription("Description");
x.setId(0l);
x.setParent(null);
x.setTittle("Tittle ...");
return x;
} catch (Exception ex) {
Logger.getLogger(LocationService.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
如果我把它放在我的浏览器中,我 100% 确定我的休息是正常的:
http://localhost:8080/BIReportL-war/rest/location/findlocation
我得到这个 Json 字符串:
{"description":"Description","id":0,"title":"Tittle ..."}
这笔交易在我的角度代码中,我从角度调用其余服务的代码正在执行,但它只是给了我错误部分:
app.controller('questionsController', function ($scope, $http) {
//var url = "http://localhost:8080/BIReportL-war/rest/location/findlocation";
//var url = "http://www.w3schools.com/angular/customers.php";
var url = "http://127.0.0.1:8080/BIReportL-war/json.json";
$http.get(url)
.success(
function (data, status, headers, config) {
alert("success");
})
.error(function(data, status, headers) {
alert('Repos status ' + status + ' --- headers : ' + headers);
})
.finally(
function() {
});
});
我与 cmets 有另一个本地 URL 到一个虚拟 json 文件,我可以通过该浏览器访问它,并且我得到相同的结果一个错误,奇怪的是我尝试使用这个 rest public json 文件:
http://www.w3schools.com/angular/customers.php
我成功了!!我不知道为什么,我在做什么或我有什么问题,我的意思是当我尝试使用我的本地休息服务时,我看到它在日志中被调用,这是事实,但角度客户端正在出错了。
提前感谢您的帮助!
我正在使用: *玻璃鱼V4 *角度
好吧,关于CORS问题,我只是把我的休息如下,所以这里是解决方案:
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/findlocation")
public Response findLocation() {
System.out.println("findlocation");
try {
LocationDTOx x = new LocationDTOx();
x.setDescription("Description");
x.setId(0l);
x.setParent(null);
x.setTittle("Tittle ...");
return Response.ok()
.entity(x)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
.build();
} catch (Exception ex) {
Logger.getLogger(LocationService.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
【问题讨论】:
-
你的错误实际上说明了什么?
-
你混合使用 127.0.0.1 和 localhost 吗?
-
是的,我混合了,如果我得到不同的东西,尝试一下没有问题,事实并非如此......
-
好吧,在我的错误代码中,我打印了状态,它带有一个值为 0(零)的值,我不知道是否可以获得更详细的字符串。
-
您似乎面临 CORS 问题
标签: java javascript json angularjs rest