![](/img/trans.png)
[英]Jersey + Grizzly HTTP + Jackson (MessageBodyWriter not found for media type=application/xml)
[英]Jersey 2.x post call MessageBodyWriter not found for media type=application/xml
我在Tomcat版本8.5上使用Jersey版本2.29 / java 1.8,並嘗試從球衣其余的post service服務中調用hasmap<String,String>
。
當我嘗試寫入hasmap作為響應時,服務器上出現異常。
2019年8月23日10:20:47 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteTo SEVERE:找不到針對媒體type = application / xml,type = class java.util.LinkedHashMap,genericType = java的MessageBodyWriter。 util.Map。
以下是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;
}
我嘗試了幾種在Internet上找到的解決方案,但是似乎沒有任何解決方案。
有人可以告訴我我在上面的代碼片段中正在做什么嗎?
我可以通過創建以下包裝器類來部分解決此問題。
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下面使用上面的類,返回typeof Map可以正常工作。
@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作為響應時無法正常工作?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.