找不到针对媒体type = application / xml

时间:2019-08-23 17:34:16

标签: java xml jaxb jersey-2.0 jersey-client

我在Tomcat 8.5版上使用泽西版2.29 / java 1.8,并试图从球衣休息后服务电话撤回hasmap<String,String>

当服务器尝试编写hasmap作为响应时,我在服务器上遇到异常。

2019年8月23日10:20:47 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteTo 严重:找不到媒体type = application / xml,type = class java.util.LinkedHashMap,genericType = java.util.Map的MessageBodyWriter。

下面是pom.xml,服务器和jersey客户端代码的详细信息。

pom.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
</dependency>
 <dependency> 
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId> 
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId> 
    <artifactId>jaxb-impl</artifactId> 
    <version>2.3.1</version> 
</dependency> 
<dependency> 
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId> 
    <version>2.3.1</version> </dependency> 
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>${jersey.version}</version>
</dependency>

客户代码

ClientConfig configuration=new ClientConfig();
Client restClientConfig = ClientBuilder.newClient(configuration);
WebTarget webTarget=restClientConfig.target("http://localhost:8080/messenger/webapi/messages/testMap");
HashMap<String,String> mapStr=new HashMap<String,String>();     
mapStr.put("a","1");
mapStr.put("b","2");
webTarget.request()
.accept(MediaType.APPLICATION_XML)
.post(Entity.json(mapStr));

Map<String,String> responseMap = new HashMap<String,String>();
GenericType<Map<String,String>> entity = new GenericType<Map<String,String>>() {};
Response xmlResponse = Response.ok(entity).build();
System.out.println("XMLResponse Is :" + xmlResponse + ":"+ responseMap.size());

泽西邮政服务代码

@POST
@Path("/testMap")
@Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Map<String,String>  postMapMessage(Map<String,String> mapMessage) {
    System.out.println("It is been invoked....and this time we will add the new MapMessage");
    if(mapMessage!=null)
    {
        System.out.println("Size of the Map Message:" + mapMessage.size());
        mapMessage.put("c","3");
    }
    return mapMessage;
}

我尝试了几种在互联网上找到的解决方案,但是似乎没有任何解决办法。

有人可以告诉我我在上面的代码片段中在做什么错吗?

1 个答案:

答案 0 :(得分:0)

我可以通过创建以下包装器类来部分解决此问题。

import java.util.Map;
import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class JaxrsMapWrapper<T,K> {

        private Map<T,K> map;

        public JaxrsMapWrapper(){

        }

        @Override
        public String toString() {
            return  map .toString();
        }

        public void setMap(Map<T,K> map) {
            this.map = map;
        }

        public Map<T,K> getMap() {
            return map;
        }
    }

通过使用getservice下面的上述类,返回Map的type绝对可以正常工作。

@GET
@Path("/mapWarpperReceive")
@Produces({MediaType.APPLICATION_XML})
public JaxrsMapWrapper<String,String> getWarpperMapMsgStr()
{

System.out.println("Returning the MapMessage as String ");
Map<String,String> originalMap=new  HashMap<String,String>(){{put("a","a");put("b","b");}};
JaxrsMapWrapper<String,String> jaxRsMapWrapper=new JaxrsMapWrapper<>();
jaxRsMapWrapper.setMap(originalMap);
return jaxRsMapWrapper;

}

但是当我尝试使用与Map类型相同的类JaxrsMapWrapper时,通过邮递员调用时会引发错误500内部服务器错误。

@GET
@Path("/customMap")
@Produces({MediaType.APPLICATION_XML})
public JaxrsMapWrapper<String,BookBo> getWarpperMapMsgWithCustomObject()
{
System.out.println("Returning the MapMessage as String and Custom Message ");
Map<String,BookBo> originalMap=new  HashMap<>();
originalMap.put("a",new BookBo(1,"Jinesh"));
JaxrsMapWrapper<String,BookBo> jaxRsMapWrapper=new JaxrsMapWrapper();
jaxRsMapWrapper.setMap(originalMap);
return jaxRsMapWrapper;
}

下面是用户定义的Java对象BookBo的代码。

@XmlRootElement
public class BookBo implements Serializable{

    private Integer id;
    private String name;

    public BookBo() {

    }

    public BookBo(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    //getters and setters of the field
} 

我在上面的代码中缺少什么,因为在编写Map作为响应时不起作用?