2016-04-25 44 views
0

我想用球衣製作一個示例REST服務器。我能夠retrun一個簡單的字符串,但是當我試圖返回數組我得到這個錯誤返回一個球衣陣列

A message body writer for Java class [C, and Java type class [C, and MIME media type application/json was not found. 

我也嘗試添加@XmlRootElement但問題依然存在。這是我的代碼:

@Path("/test") 

public class Test { 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public char[] getHello() { 

     char[] test = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 
       'i', 'n', 'a', 't', 'e', 'd' }; 
     return test; 
    } 
} 

編輯:pom.xml中添加

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>HospitalServer</groupId> 
    <artifactId>HospitalServer</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <repositories> 
     <repository> 
      <id>maven2-repository.java.net</id> 
      <name>Java.net Repository for Maven</name> 
      <url>http://download.java.net/maven/2/</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 

    <dependencies> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>1.8</version> 
     </dependency> 

    </dependencies> 

    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.3</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.6</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

請顯示你的'pom.xml'。 –

+0

文章編輯,pom.xml添加 – user3235881

+0

只是好奇,爲什麼你使用澤西最古老的版本之一的最新的Java版本? –

回答

1

你仍然需要一個JSON提供程序來處理對象JSON序列化。你不需要一個字符串,因爲序列化字符串輸出流是微不足道的,所以澤西島可以自己處理。因此,只要添加以下

<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-json</artifactId> 
    <version>1.8</version> 
</dependency> 

然後在web.xml

<init-param> 
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
    <param-value>true</param-value> 
</init-param> 

配置它添加到您的新澤西servlet配置。此外,您可能需要this also

一旦你有這個,你仍然無法處理char[]。這只是傑克遜無法處理這種類型的問題。但它可以處理String[]List<String>List<Character>或POJO,以及您將需要的幾乎任何其他類型。我不太清楚,但我認爲它可能只是原始標量數組,它有一個問題。

0

嘗試使用字符[]類型,而不是的char []

+0

它不能解決問題 – user3235881

0

超級原始測試,如果不是必須使用char數組嘗試將類型更改爲String,即使發送單個字符也是如此。 它可能是與焦炭編組問題

@Path("/test") 
public class Test { 

@GET 
@Produces(MediaType.APPLICATION_JSON) 
public List<String> getHello() { 
    return Arrays.asList("a", "b"); 
} 

記住改爲雙引號

+0

這不是強制性的,我只是想舉個例子。我複製了你的代碼,它不工作,我重試了一個簡單的字符串,它再次工作。 – user3235881

+0

同樣的錯誤?我沒有嘗試代碼...也許我犯了一些愚蠢的錯誤 – rick

+0

簡化了測試 – rick