错误:GWt requestfactory中的ConstraintViolation

时间:2015-06-16 09:37:09

标签: gwt requestfactory

您好我正在尝试使用requestfactory,看看它是否比RPC更好。

每当我尝试运行我的GWT应用程序时,我都会遇到错误 -

[ERROR] [exampledataextractrf] Uncaught exception escaped

com.google.web.bindery.event.shared.UmbrellaException: Exception caught: The call failed on the server due to a ConstraintViolation
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext.violation(AbstractRequestContext.java:799)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$StandardPayloadDialect.processPayload(AbstractRequestContext.java:369)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$5.onTransportSuccess(AbstractRequestContext.java:1161)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport$1.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347)
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: The call failed on the server due to a ConstraintViolation
    at com.google.web.bindery.requestfactory.shared.Receiver.onFailure(Receiver.java:44)
    at com.google.web.bindery.requestfactory.shared.Receiver.onViolation(Receiver.java:66)
    at com.google.web.bindery.requestfactory.shared.Receiver.onConstraintViolation(Receiver.java:107)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequest.onViolation(AbstractRequest.java:138)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext.violation(AbstractRequestContext.java:779)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$StandardPayloadDialect.processPayload(AbstractRequestContext.java:369)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$5.onTransportSuccess(AbstractRequestContext.java:1161)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport$1.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347)
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

以下是我尝试使用的onClick代码

final Button buttonMessage = new Button("Click to get the complete list of Employees!!!");
    txtName.setWidth("200");
    logger.info(txtName.getValue());
    buttonMessage.addClickHandler(new ClickHandler() {  
          public void onClick(ClickEvent event) {
              buttonMessage.setEnabled(false);
              dataFlexTable.removeAllRows();
              dataFlexTable.setText(0, 0, "Employee ID");
              dataFlexTable.setText(0, 1, "Employee Name");
              dataFlexTable.setText(0, 2, "Department name");
              context.findAllEmployees().fire(
                      new Receiver<List<EmployeeProxy>>(){
                          @Override
                          public void onSuccess(List<EmployeeProxy> emp){
                              logger.info("Inside onSuccess");
                              addEmployee(emp);
                              txtName.setEnabled(true);
                              txtName.setText("");
                          }
                      });

          }});

我的EmployeeRequestfactory类如下 -

public interface EmployeeRequestFatory extends RequestFactory {
    @Service(value=EmployeeDAO.class, locator=DaoLocator.class)
    public interface EmployeeRequestContext extends RequestContext{
        Request<List<EmployeeProxy>> findAllEmployees();
        Request<List<EmployeeProxy>> findEmployee(String name);
    }
    EmployeeRequestContext context();
}

findAllEmployees()方法如下 -

public List<Employee> findAllEmployees(){
        System.out.println("Inside Main Method");
        Connection con = null;
        ResultSet rs = null;
        Statement stmt=null;
         List<Employee> emp=new ArrayList<Employee>();
            try{  
                con = connectionUtil.getConnection();
                stmt = con.createStatement();
                rs = stmt.executeQuery("select * from Employee");
//              logger.log(Level.INFO,rs.toString());
                if(rs!=null){        
                    while(rs.next()) {
                        Employee employee=new Employee();
                        employee.setId(rs.getLong("EMP_ID"));
                        employee.setName(rs.getString("EMP_NAME"));
                        employee.setDepName(rs.getString("DEPT_NAME"));
                        emp.add(employee);
                    }
                }
        } catch (Exception e){
//          logger.log(Level.INFO,e.toString());
                e.printStackTrace(); 
        }finally{
            connectionUtil.closeResources(con, stmt, rs);
        }
            return emp;

    }

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

在客户端,如果无法验证发送到服务器的对象,则调用Receiver#onConstraintViolation

这意味着您的代码中还有其他内容可以将对象发送到服务器(基本上是在RequestContext上调用create()edit(),但它也可以是一个调用你的另一个方法,而不是带有参数的findAllEmployees,并且在服务器上你有一个JSR-303验证器(例如Hibernate Validator)来判断该对象是无效的。这是在您的服务器端findAllEmployees被调用之前完成的。

要对此进行调试,请在ReflectiveServiceLayer#validate(服务器端)内设置断点并查找非空返回值。
在客户端,您可能希望在AbstractRequestContext#takeOwnership内设置断点并查看callstack以确定代码中的哪些内容导致将对象发送到服务器。