我希望我的服务器向角度js客户端发送指示性错误。
我看到了这些例子:
Example 7
Project: spice File: RequestService.java View source code 6 votes vote down vote up
@GET
@Path("date")
@Produces("text/plain")
public Response get(@Context Request request) {
final Date modificDate = getLastModificationDateFromDatastore();
final EntityTag entityTag = getEntityTagFromDatastore();
final ResponseBuilder resp = request.evaluatePreconditions(modificDate,
entityTag);
if (resp != null) {
return resp.build();
}
// Build new Response
final ResponseBuilder responseBuilder = Response.status(200);
responseBuilder.entity("This is the Entity from " + modificDate);
responseBuilder.lastModified(modificDate);
responseBuilder.tag(entityTag);
return responseBuilder.build();
}
和
Example 8
Project: zeppelin File: JsonResponse.java View source code 4 votes vote down vote up
public javax.ws.rs.core.Response build() {
ResponseBuilder r = javax.ws.rs.core.Response.status(status).entity(this.toString());
if (cookies != null) {
for (NewCookie nc : cookies) {
r.cookie(nc);
}
}
return r.build();
}
但是当我的服务器代码发送时:
catch (Exception ex)
{
String error = "RuntimeException: test11";
logger.error("update voice filed. Error: "+error);
return Response.serverError().entity(error).build();
}
在客户端代码中我找不到此错误消息:
$http.put('api/foo', {voice : voice, isAddMode : isAddMode}).then(
function successCallback(response) {
..
}, function errorCallback(response) {
var responseStr = JSON.stringify(response)
console.log(responseStr);
deferred.reject(responseStr);
});
我得到解析错误,但一般来说我找不到js中的正确成员。
SyntaxError: Unexpected token R in JSON at position 0
at JSON.parse (<anonymous>)
at fromJson (angular.js:1377)
at defaultHttpResponseTransform (angular.js:11003)
at angular.js:11094
at forEach (angular.js:357)
at transformData (angular.js:11093)
at transformResponse (angular.js:11914)
at processQueue (angular.js:16648)
at angular.js:16692
at Scope.$eval (angular.js:17972)
答案 0 :(得分:0)
我不得不删除服务器端的"Produces"
注释:
@Path("/foo")
@PUT
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
// @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
然后从服务器发送了一个json对象,而不只是字符串。