Error:ConstraintViolation in GWt requestfactory

Error:ConstraintViolation in 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.onTransportSuccess(AbstractRequestContext.java:1161)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
    at com.google.gwt.http.client.RequestBuilder.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.onTransportSuccess(AbstractRequestContext.java:1161)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:259)
    at com.google.gwt.http.client.RequestBuilder.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 class 如下 -

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;

    }

任何帮助将不胜感激:)

在客户端,Receiver#onConstraintViolation 被称为“如果无法验证发送到服务器的对象”。

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

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