RestEasy:可以发布两个复杂的对象吗?

时间:2013-03-25 08:25:43

标签: forms post resteasy

我正在尝试使用带有@POST注释的RestEasy将两个复杂对象作为参数传输。

@POST
@Path("/save")
@Consumes("application/json")
public void save(ComplexeObjectAleph objectAleph, ComplexeObjectBeth objectBeth);

只有一个参数是好的,但有两个 - 我得到例外:

Caused by: java.io.EOFException: No content to map to Object due to end of input
    at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2691)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:63)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
    at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:169)
    at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:136)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:159)
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542)
    ... 22 more

示例:https://github.com/markstein/de.mosst/tree/master/PlayareaRestEasy

1 个答案:

答案 0 :(得分:0)

这是可能的,但要谨慎。

这两个参数必须使用@Form进行注释。

@Path("playarea")
public interface PlayareaRsInterface {

    @POST
    @Path("save")
    @Produces(MediaType.APPLICATION_JSON)
    public void save(@Form ComplexeObjectAleph objectAleph, @Form ComplexeObjectBeth objectBeth);

}

并且这两个类的属性必须具有@FormParam(...)注释:

public class ComplexeObjectAleph {

    @FormParam("aleph")
    public String aleph;

}

public class ComplexeObjectBeth {

    @FormParam("beth")
    public String beth;

}

但请注意,@FormParam(...)的值必须不相等,否则RestEasy无法正确解析数据。可能是RestEasy的错误。