泽西岛:如何返回List <mytype <k,v>&gt;作为回应?</mytype <k,v>

时间:2012-02-10 13:04:37

标签: java xml rest jaxb jersey

我有以下网络服务:

 @Path("/storage-dirs/{user}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public JResponse<List<MarshalledMapEntry<String, Integer>>> getStorageDirs(@PathParam("user") String user) {


        //List<MarshalledMapEntry<String, Integer>>
        List<MarshalledMapEntry<String, Integer>> myEntries = new ArrayList<MarshalledMapEntry<String, Integer>>();
        Map<String, Integer> originalEntries = DBAccessor.getStorageDirsForUser(user);

        for (Map.Entry<String, Integer> en : originalEntries.entrySet()) {

            myEntries.add(new MarshalledMapEntry<String, Integer>(en.getKey(), en.getValue().intValue()));
        }

        //GenericEntity<List<MarshalledMapEntry<String, Integer>>> entity = new GenericEntity<List<MarshalledMapEntry<String, Integer>>> {};
       return JResponse.ok(myEntries).build();
    }

这是我的MarshalledMapEntry:

@XmlRootElement
    public class MarshalledMapEntry<K, V> {

    @XmlElement
    private K key;
    @XmlElement
    private V value;

    MarshalledMapEntry() {} //JAXB requires it

    public MarshalledMapEntry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

}

由于JAXB不支持将集合作为顶级元素,因此我编写了一个包装地图的简单类。但我得到以下例外:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<silverline.MarshalledMapEntry<java.lang.String, java.lang.Integer>>, and MIME media type application/xml was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

很明显,我试图使用JResponse来避免List&lt;&gt;的类型擦除。但由于我在Marshalled课上有仿制药,我相信这会引起一个问题。因此,如果我希望能够返回Generic Maps,我可以实现什么解决方案。如果我改变我的MarshalledMapEntry,以便我不使用泛型,而是使用String,String作为它的类型,但我可以有几种不同的Map类型,我不想创建只有更改类型的这个类的副本? / p>

1 个答案:

答案 0 :(得分:0)

你需要实现

@Provider
@Produces(MediaType.APPLICATION_XML)
public class ASDFWriter implements MessageBodyWriter<String>
{

    @Override
    public long getSize(String arg0, Class<?> arg1, Type arg2,
            Annotation[] arg3, MediaType arg4)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
            MediaType arg3)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void writeTo(String arg0, Class<?> arg1, Type arg2,
            Annotation[] arg3, MediaType arg4,
            MultivaluedMap<String, Object> arg5, OutputStream arg6)
            throws IOException, WebApplicationException
    {
        // TODO Auto-generated method stub

    }

}

并将其注册到web.xml

<web-app>
    <servlet>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>
                    your.package.with.providers
                </param-value>
            </init-param>
        </servlet>
    </web-app>