0

我想为我的球衣服务实现一个 restlet 客户端。我知道该服务有效,因为我已经为它开发了一个球衣客户端。但似乎球衣和休息之间的沟通有问题。

泽西服务

资源:

 @Path("/object")
 @RolesAllowed({"admin", "user"})
 public class ObjectResourceBean implements ObjectResourceIF {
        @POST
        @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        public Response postObject(JAXBElement<ObjectDTO> object) {
            ObjectDTO c = object.getValue();
            String generatedId = generateID();
            c.setObjectId(generatedId);
            c.setOwner(sec.getUserPrincipal().getName());
            return postAndGetResponse(c);
        }

        private Response postAndGetResponse(ObjectDTO object) {
            Response res;

                res = Response.created(UriBuilder.fromUri(uriInfo.getAbsolutePath() + "/" + object.getObjectId()).build()).entity(object).build();
                ObjectDAO.instance.getObjectDao().put(object.getObjectId(), object);

                System.out.println("Response: " + res);
            }
            return res;
        }

Restlet Wrap 接口:

    @Path("/object")
    @RolesAllowed({"admin", "user"})
    public interface ObjectResourceIF {
        @Post
        public Response postObject(JAXBElement<ObjectDTO> object);
 }

已经在这里,我不能使用像 @POST 这样的 Jersey 注释。我必须使用 Restlet Framework 中的 @Post。但这不是真正的问题。

Restlet 客户端

 ClientResource service = new ClientResource("http://localhost:8080/com-project-core/rest");
 service.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "admin", "xxx");
 service.getReference().addSegment("object");
 // *create object*
 JAXBElement<ObjectDTO> object = new JAXBElement<ObjectDTO>(new QName("object"),    ObjectDTO.class, objectOne);
    ObjectResourceIF objectResource = service.wrap(ObjectResourceIF.class);

    List<Preference<MediaType>> acceptedMediaTypes = new ArrayList<Preference<MediaType>>();
    acceptedMediaTypes.add(new Preference(MediaType.APPLICATION_JSON));
    service.getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes);

    Response res = objectResource.postObject(object);

我刚刚收到内部服务器错误 (500)。

    Internal Server Error (500) - The server encountered an unexpected condition which prevented it from fulfilling the request
    at org.restlet.resource.UniformResource.toObject(UniformResource.java:649)
    at org.restlet.resource.ClientResource$1.invoke(ClientResource.java:1669)
    at $Proxy12.postObject(Unknown Source)
    at com.project.restlet.RestletConnectedTest.postObject(RestletConnectedTest.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: objectId : objectId
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:71)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:86)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:96)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:52)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:136)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:861)
    at org.restlet.ext.xstream.XstreamRepresentation.getObject(XstreamRepresentation.java:166)
    at org.restlet.ext.xstream.XstreamConverter.toObject(XstreamConverter.java:200)
    at org.restlet.service.ConverterService.toObject(ConverterService.java:161)
    at org.restlet.resource.UniformResource.toObject(UniformResource.java:647)
    ... 27 more

在服务器端,请求完成

 return res;

当我尝试它而不用包装接口时

 service.post(object, MediaType.APPLICATION_JSON).write(System.out);

有用!

正如我所说,泽西客户端也可以工作。

也许转移的类 ObjectDTO 对解决方案很有用:

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ObjectDTO implements Serializable{

        /**
         * 
         */
        private static final long serialVersionUID = -8545841080597549468L;

        @XmlElement(name="objectId")
        private String objectId;
        @XmlElement(name="owner")
        private String owner;
        @XmlElement(name="objectName")
        private String objectName;

        public ObjectDTO() {

        }

        public ObjectDTO(String objectName) {
            this.objectName = objectName;
        }

        public String getObjectId() {
            return objectId;
        }

        public void setObjectId(String objectId) {
            this.objectId = objectId;
        }

        public String getOwner() {
            return owner;
        }

        public void setOwner(String owner) {
            this.owner = owner;
        }
 }

我也试过没有可序列化的。也没有@XmlElement 和@XmlAccessorType(XmlAccessType.FIELD)。我也试过了

  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})

界面上的注释。

4

1 回答 1

1

我们在 Jersey 服务器和 Restlet 客户端上遇到了同样的问题,但是有一个 get 请求。使用 Restlet 客户端与 Restlet 服务器通信,我们没有任何问题。所以我们可以看到的唯一区别是,Restlet 服务器实现将编组 POJO 的完整限定类名称包装为 JSON 对象,围绕包含 POJO 有效负载的 JSON 对象。

package com.xyz;

    public class ExamplePojo implements Serializable{

        private static final long serialVersionUID = 1L;

        private String var;

        public ExamplePojo(){

        }
    //... 
    }

Restlet 服务器上生成的 json 字符串:

{"com.xyz.ExamplePojo":{"var":"10:23:53 30.10.2012"}}

但是如果没有生成 MediaType.APPLICATION_JSON,则 Jersey 服务器在其默认实现中。泽西服务器上生成的 json 字符串:

{"var":"10:23:53 30.10.2012"}

问题是,如果我们尝试通过 Restlet 客户端与 Jersey 服务器进行通信,我们会遇到与您的堆栈跟踪相同的错误,其根本原因是:

Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: var : var

我们的解决方案是,如果使用带有 Jersey 服务器实现的 Restlet 客户端,我们必须明确定义 JacksonConverter。

因此,我们将 Restlet zip 中的库放在客户端项目的构建路径中

org.restlet.ext.jackson.jar     
org.codehaus.jackson.core.jar           
org.codehaus.jackson.mapper.jar

(资源接口)

public interface PojoResource {
    @Get("json")
    public ExamplePojo retrieve();
}

然后我们将显式 JacksonConverter 添加到已注册的转换器中

public ExamplePojo doRequest(){
    ClientResource cr = new ClientResource( url ); 
    // this is essential for Restlet-Jersey marshalling  
    Engine.getInstance().getRegisteredConverters().add( 0, new JacksonConverter() ); 
    PojoResource resource = cr.wrap( PojoResource.class );
    return resource.retrieve();
}

一切正常。

请注意,显式调用

Engine.getInstance().getRegisteredConverters().add( 0, new JacksonConverter() ); 

在 Reslet-Client 上与 Restlet-Server 的通信不起作用,但导致

org.codehaus.jackson.map.JsonMappingException

因为将 JSON 对象“com.xyz.ExamplePojo”包装为无法识别的字段,必须将其标记为可忽略。

因此,在您的情况下,Restlet 似乎也没有找到用于解组的转换器,因此请尝试指定一个明确的转换器。

于 2012-10-30T11:56:43.047 回答