Errai-jaxrs - 不良请求

时间:2013-05-10 09:42:00

标签: java gwt resteasy errai

我在使用我的应用程序运行Errai-jaxrs时遇到了一个问题我从GWT开发模式中得到了这个错误:

org.jboss.errai.enterprise.client.jaxrs.api.ResponseException: Bad Request    
at org.jboss.errai.enterprise.client.jaxrs.AbstractJaxrsProxy$1.onResponseReceived(AbstractJaxrsProxy.java:132)    
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)    
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)    
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    
at java.lang.reflect.Method.invoke(Method.java:474)    
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:337)    
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)    
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)    
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)    
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)    
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:213)    
at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)    
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    
at java.lang.reflect.Method.invoke(Method.java:474)    
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:292)    
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)    
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)    
at java.lang.Thread.run(Thread.java:722)

在服务器端:

[INFO] Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "^EncodedType" (Class com.mycompany.client.shared.Customer), not marked as ignorable
[INFO]  at [Source: org.mortbay.jetty.HttpParser$Input@115194d; line: 1, column: 18] (through reference chain: com.mycompany.client.shared.Customer["^EncodedType"])
[INFO]  at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
[INFO]  at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
[INFO]  at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
[INFO]  at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
[INFO]  at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
[INFO]  at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
[INFO]  at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
[INFO]  at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
[INFO]  at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)

从堆栈跟踪看起来像是错误的请求。这个的后端是RestEasy + Guice,它配置得非常好,因为在我尝试使用errai-jaxrs之前,我只是使用普通的GWT RequestBuilder工作正常。我不确定问题出在哪里,但这是我的代码:

CustomerService.java

@Path("/customerservice")
public interface CustomerService {
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)     
    public Customer getCustomer(String id);

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String createCustomer(Customer Customer);
}

CustomerServiceImpl.java

@Singleton
public class CustomerServiceImpl implements CustomerService {
    @Override
    public Customer getCustomer(String id) {
        return new Customer();
    }

    @Override
    public String createCustomer(Customer Customer) {
        return "1";
    }
}

在客户端:

@Inject
private Caller<CustomerService> customerService;

@EventHandler("submit")
public void createCustomer(ClickEvent event) {   
    event.preventDefault();
    Customer customer = new Customer();
    customerService.call(new RemoteCallback<String>() {
        @Override
        public void callback(String response) {
            Window.alert("Customer created: " + response);
        }
    }).createCustomer(customer);
}

可能是什么问题?

1 个答案:

答案 0 :(得分:4)

我可以从堆栈跟踪中看到您正在使用Jackson在服务器端编组请求和响应数据,但客户端正在使用Errai的默认编组格式。

您必须决定是否要使用Errai的编组格式或Jackson。

Errai Marshalling

Errai的编组格式的优势在于它在线路上包含更多类型信息(例如,您看到Jackson解析器抱怨的“^ EncodedType”属性)。当您的JAX-RS API主要用作Errai客户端时,这是最佳选择。它也是唯一的方式,你可以通过网络发送这样的对象:

@Portable class TypeWithPolymorphicField {
  private Object foo = new Foo();
}

要在服务器端启用Errai编组(而不是Jackson),您需要在应用中部署errai-jaxrs-provider-[version].jar。来自Errai Reference Guide

<dependency>
  <groupId>org.jboss.errai</groupId>
  <artifactId>errai-jaxrs-provider</artifactId>
  <version>${errai.version}</version>
</dependency>

Jackson Marshalling

另一方面,如果您的JAX-RS API旨在供非Errai客户使用,他们可能会发现Jackson编组更加愉快和熟悉。如果选择,则需要告诉客户端使用Jackson编组而不是默认编组。来自Errai Reference guide

  

以下选项可用于激活杰克逊编组   在客户端上。请注意,这是客户端配置,   假设JAX-RS端点已经返回Jackson表示   (杰克逊受到所有JAX-RS实施的支持)。该   errai-jaxrs-provider.jar不必部署在服务器上   这个案子!

     
<script type="text/javascript">
  erraiJaxRsJacksonMarshallingActive = true;
</script>
     

或使用JSNI方法:

     
private native void setJacksonMarshallingActive(boolean active) /*-{  
  $wnd.erraiJaxRsJacksonMarshallingActive = active;
}-*/;
     

或简单地调用:

RestClient.setJacksonMarshallingActive(true);

最后注意事项:不要做上述两种情况,否则你会遇到相反的问题!您必须将客户端服务器配置为远离其自然默认值。