0

I am trying to make a sample REST server with jersey. I am able to retrun a simple String but when I try to return an Array I get this error

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

I also tried to add @XMLRootElement but the problem remain. This is my code:

@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;
    }
}

Edit: pom.xml added

<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>
user3235881
  • 487
  • 2
  • 9
  • 24

3 Answers3

1

You still need a JSON provider to handle Object to JSON serialization. You don't need one for String, as serializing String to output stream is trivial, so Jersey can handle that by itself. So just add the following

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

Then configure it in your web.xml

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

Add this to your Jersey servlet configuration. Also, it may be possible that you need this also.

Once you have this, you still will not be able to handle char[]. This is just a problem with Jackson not being able to handle that type. But it can handle String[] or List<String>, List<Character> or POJOs, and a almost any other type you will need. I am not quite sure, but I think it might just be primitive scalar arrays, it has a problem with.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Try to use Character[] type instead of char[]

Maciej Laskowski
  • 697
  • 1
  • 5
  • 7
0

Super raw test, if it's not mandatory to use an array of char try to change the type to String even if you send single characters. it might be a problem with the char marshaller

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

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

Remember to change to doublequotes

rick
  • 1,869
  • 13
  • 22
  • It's not mandatory, I just want to make an example. I copied your code and it's not working, I retried with a simple String and it worked again. – user3235881 Apr 25 '16 at 19:23
  • Same error? I didn't try the code... Maybe I made some stupid mistake – rick Apr 25 '16 at 19:25
  • Still have the same error even after the simplification: A message body writer for Java class java.util.Arrays$ArrayList, and Java type java.util.List, and MIME media type application/json was not found – user3235881 Apr 25 '16 at 19:38
  • at this point it can be a problem of dependencies. try to reproduce something like this http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ – rick Apr 25 '16 at 19:45