如何处理使用REST服务的客户端 - 服务器应用程序错误?如何处理、应用程序、客户端、错误

由网友(同归)分享简介:问 1)如何使用REST服务时,在处理客户端 - 服务器应用程序错误?可能有人帮助我,例如$ C C什么是处理在下面code错误信息的最佳途径$。如何捕捉在客户端和/或服务器侧的异常的情况下创建服务例如? 2)如何转发用户到不同的页面错误的情况下,并在成功的情况下?客户端 $('#btnSignup)。点击(函数()...

1)如何使用REST服务时,在处理客户端 - 服务器应用程序错误?可能有人帮助我,例如$ C C什么是处理在下面code错误信息的最佳途径$。如何捕捉在客户端和/或服务器侧的异常的情况下创建服务例如?

2)如何转发用户到不同的页面错误的情况下,并在成功的情况下?

客户端

  $('#btnSignup)。点击(函数(){
            VAR用户= {ID:10,名字:$('#的firstName)VAL(),姓氏:$('#的lastName)VAL(),时代:69,自由文本:55555,重:55};

            $阿贾克斯({
            键入:POST,
            的contentType:应用/ JSON
            网址:HTTP://本地主机:8080 /测试/ webresources / entity.user /,
            数据类型:JSON,
            数据:JSON.stringify(用户),

            成功:函数(响应){
               $('#myModal)模式(隐藏)。
            },
            错误:功能(数据){
                警报('ADDUSER错误:');
            }
        });

    });
 

REST服务

  @Stateless
@Path(entity.user)

公共类UsersFacadeREST扩展AbstractFacade<用户> {
@PersistenceContext(的unitName =testPU)
私人EntityManager的EM;

公共UsersFacadeREST(){
    超(Users.class);
}

@POST
@覆盖
@Consumes({应用程序/ XML,应用/ JSON})
公共无效创建(用户单位){
    super.create(实体);
}
 

解决方案

只是我的2分钱 那么你可以定义一个结果对象,你可以每天休息呼叫后返回

 类结果{
   私人字符串状态code;
   私人字符串状态说明;

}
 
应用程序中的服务器错误怎么办

在豆

  @POST
@覆盖
@Consumes({应用程序/ XML,应用/ JSON})
@Produces({应用/ JSON})
公开的结果中创建(用户单位){
  结果结果=新的结果();
  尝试{
    super.create(实体);
   result.setStatus(200);
   result.setStatusDescriptions(成功);
   }赶上(例外){
        result.setStatus(500);
        result.setStatusDescriptions(失败);
   }

  返回结果; //返回此为JSON
}
 

您可以稍后解析HTML的响应使用jQuery和处理基础上的状态

流量

QUESTION

1) How to handle errors in client-server application when using REST services? Could somebody help me with example code what is the best way to handle error messages in code below. How to catch exceptions in client side and/or server side in case of create-service for example?

2) How to forward user to different page in case of error and in case of success?

CLIENT

 $('#btnSignup').click(function(){ 
            var user = {"id": 10, "firstName": $('#firstName').val(), "lastName":       $('#lastName').val(), "age": 69, "freeText": "55555", "weight": 55};

            $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "http://localhost:8080/test/webresources/entity.user/",
            dataType: "json",
            data: JSON.stringify(user),

            success: function(response) {
               $('#myModal').modal('hide');
            },
            error: function(data) {
                alert('addUser error: ');
            }
        });

    }); 

REST SERVICE

@Stateless
@Path("entity.user")

public class UsersFacadeREST extends AbstractFacade<Users> {
@PersistenceContext(unitName = "testPU")
private EntityManager em;

public UsersFacadeREST() {
    super(Users.class);
}

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Users entity) {
    super.create(entity);
}

解决方案

Just my 2 cents Well you can define a Result Object , which you can return after every rest call

Class Result{
   private String statusCode;
   private String statusDescription;

}

IN the bean

@POST
@Override
@Consumes({"application/xml", "application/json"})
@Produces({ "application/json"})
public Result create(Users entity) {
  Result result = new Result();
  try{
    super.create(entity);
   result.setStatus("200");
   result.setStatusDescriptions("Success");
   }catch(Exception) {
        result.setStatus("500");
        result.setStatusDescriptions("Fail");
   }

  return result;  // Return this as Json 
}

You can later parse the response in HTML using jquery and handle the flow based on the 'status'

阅读全文

相关推荐

最新文章