RDM
RDM

Reputation: 1191

REST: MessageBodyWriter not found for media type=text/plain

I am currently learning REST and I'm facing an error while calling a method. I am sure that it must be a simple configuration setting, but since I am absolutely new to learning REST, not able to figure it out.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.mishra.dev.rahul.messenger</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.mishra.dev.rahul</groupId>
    <artifactId>messenger</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>messenger</name>

    <build>
        <finalName>messenger</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.26-b03</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

resource class:

package org.mishra.dev.rahul.messenger.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("messages")
public class MessageResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Person getMessages() {
        return new Person("Rahul", 31);
    }

}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }
}

Facing an error while calling: http://localhost:8080/messenger/webapi/messages

Error:

Jun 09, 2017 7:19:01 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/plain, type=class org.mishra.dev.rahul.messenger.resources.Person, genericType=class org.mishra.dev.rahul.messenger.resources.Person.

Now, if instead of returning Person object, if I return a plain string like "Hello World", I do not face any issues.

EDIT: **Solution:**I was missing the default constructor in the Person class. After adding the default constructor, I can now return the string format of the Person object.

However having said that, if I change @Produces(MediaType.APPLICATION_JSON) and return return new Person("Rahul", 31), I just see an empty JSON. I am not sure why dont I see the Person object in JSON format.

Upvotes: 1

Views: 11512

Answers (2)

Shikhar Chaudhary
Shikhar Chaudhary

Reputation: 513

Well, I was also facing the same issue and the thing that worked for me, in this case, was that I annotated the API returning the response with this

@Produces(MediaType.APPLICATION_JSON)

There is no need for you to define the setter unnecessarily as it breaks the mutability of the object.

Upvotes: 6

RDM
RDM

Reputation: 1191

I read the documentation of Jersey and I found out that I need to make two changes in my resource class:

  1. Declare the default constructor
  2. Define getters and setters for the instance variables.
  3. Add the following dependency in pom.xml

    <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>
    

    Now after making these changes I am able to view the string and JSON format of the Person object.

Upvotes: 3

Related Questions