Hi I have a simple Rest service:
嗨,我有一个简单的休息服务:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Product createProduct(Product product) {
if (!(productDao.findByName(product.getProductName()).isEmpty())) {
//?????
} else {
productDao.create(product);
//?????
}
return product;
}
When input name is incorrect and method findByName return not null I want return from my rest service method to angular only information, example "Product exist". When method findByName return null and product is created I want return Product from my method to Angular controller. How handle it? Return entity and information?
当输入名称不正确并且方法findByName返回非null时,我希望从我的rest服务方法返回到仅角度信息,例如“Product exists”。当方法findByName返回null并且创建了product时,我想从我的方法返回Product到Angular controller。如何处理?返回实体和信息?
And what I handle it in my angular controller? Below controller wokrs good when I return entity, but I don't now why handle information "Product exist" not entity?
我在角度控制器中处理它的是什么?当我返回实体时,控制器下面很好,但我现在不知道为什么处理信息“产品存在”而不是实体?
$scope.addProduct = function (product) {
$http.post(serviceURI + "products", product).success(function (data) {
$scope.products.push(data);
$('.success-message').fadeIn(1000).delay(5000).fadeOut(1000);
$scope.message = "Product added";
}).error(function (error) {
$('.error-message').fadeIn(1000).delay(5000).fadeOut(1000);
$scope.message = "Error";
});
}
How is the best practice to return data and information from JAX-RS and get it in Angular controller?
从JAX-RS返回数据和信息并在Angular控制器中获取它的最佳实践如何?
2 个解决方案
#1
1
In my case, I use JAX-RS for the input and Gson for the output when the response is ok because I need to manipulate dates (long/Calendar) and with Gson is easy to add adapters. When the response is not ok, I return a status error code.
在我的情况下,当响应正常时,我使用JAX-RS作为输入,使用Gson作为输出,因为我需要操作日期(长/日历)并且使用Gson很容易添加适配器。当响应不正常时,我返回状态错误代码。
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Product createProduct(Product product) {
try {
Gson gson = new GsonBuilder()
//.registerTypeAdapter(Calendar.class, new CalendarSerializer())
// .registerTypeAdapter(Calendar.class, new CalendarDeserializer())
// .registerTypeAdapter(GregorianCalendar.class,
// new CalendarSerializer())
.create();
String json = null;
//do something
json = gson.toJson(product);
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}catch (IllegalArgumentException ex){
return Response.serverError().status(Status.BAD_REQUEST).build();
}catch (Exception ex) {
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
finally{
}
}
Client with AngularJs:
使用AngularJs的客户:
.success (function(data) {
//ok -> get data
})
.error (function(resp) {
if (resp.errorCode == 400){
...
}
...
...
}
#2
3
Since RESTFul webservices work with the HTTP vocabulary I suggest to return different HTTP status codes depending on the outcome of your operations.
由于RESTFul webservices使用HTTP词汇表,我建议根据您的操作结果返回不同的HTTP状态代码。
In the first case you could throw a WebApplicationException with a status code of the 4xx family.
在第一种情况下,您可以抛出WebApplicationException,其状态代码为4xx系列。
In the second case you could use the default status code (I think it would be 200 in this case) or provide a more specific status code such as 201 (Created) by returning a Response object instead of a Product directly. Eg. Response.created might help you.
在第二种情况下,您可以使用默认状态代码(我认为在这种情况下它将是200)或通过直接返回Response对象而不是Product来提供更具体的状态代码,例如201(Created)。例如。 Response.created可能对你有所帮助。
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
#1
1
In my case, I use JAX-RS for the input and Gson for the output when the response is ok because I need to manipulate dates (long/Calendar) and with Gson is easy to add adapters. When the response is not ok, I return a status error code.
在我的情况下,当响应正常时,我使用JAX-RS作为输入,使用Gson作为输出,因为我需要操作日期(长/日历)并且使用Gson很容易添加适配器。当响应不正常时,我返回状态错误代码。
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Product createProduct(Product product) {
try {
Gson gson = new GsonBuilder()
//.registerTypeAdapter(Calendar.class, new CalendarSerializer())
// .registerTypeAdapter(Calendar.class, new CalendarDeserializer())
// .registerTypeAdapter(GregorianCalendar.class,
// new CalendarSerializer())
.create();
String json = null;
//do something
json = gson.toJson(product);
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}catch (IllegalArgumentException ex){
return Response.serverError().status(Status.BAD_REQUEST).build();
}catch (Exception ex) {
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
finally{
}
}
Client with AngularJs:
使用AngularJs的客户:
.success (function(data) {
//ok -> get data
})
.error (function(resp) {
if (resp.errorCode == 400){
...
}
...
...
}
#2
3
Since RESTFul webservices work with the HTTP vocabulary I suggest to return different HTTP status codes depending on the outcome of your operations.
由于RESTFul webservices使用HTTP词汇表,我建议根据您的操作结果返回不同的HTTP状态代码。
In the first case you could throw a WebApplicationException with a status code of the 4xx family.
在第一种情况下,您可以抛出WebApplicationException,其状态代码为4xx系列。
In the second case you could use the default status code (I think it would be 200 in this case) or provide a more specific status code such as 201 (Created) by returning a Response object instead of a Product directly. Eg. Response.created might help you.
在第二种情况下,您可以使用默认状态代码(我认为在这种情况下它将是200)或通过直接返回Response对象而不是Product来提供更具体的状态代码,例如201(Created)。例如。 Response.created可能对你有所帮助。
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html