gpt4 book ai didi

java - Errai-jaxrs - 错误请求

转载 作者:行者123 更新时间:2023-12-01 18:49:01 25 4
gpt4 key购买 nike

我在使用我的应用程序运行 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);
}

可能是什么问题?

最佳答案

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

您必须决定是使用 Errai 的编码格式还是 Jackson。

Errai 编码

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 编码

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

The following options are available for activating Jackson marshalling on the client. Note that this is a client-side configuration, the JAX-RS endpoint is assumed to already return a Jackson representation (Jackson is supported by all JAX-RS implementations). The errai-jaxrs-provider.jar does not have to be deployed on the server in this case!

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

or by using a JSNI method:

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

or by simply invoking:

RestClient.setJacksonMarshallingActive(true);

最后注意:不要同时执行上述两项操作,否则您将遇到相反的问题!您必须将客户端或服务器配置为远离其自然默认值。

关于java - Errai-jaxrs - 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16479366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com