Yoseph
Yoseph

Reputation: 121

Internal Server Error - Tomcat & Jersey

I'm trying to go through the following tutorial, but I keep getting an error: https://www.tutorialspoint.com/restful/restful_first_application.htm

When I try to acces the following link I get an HTTP Status 500 - Interval Server Error: http://localhost:8080/NoteMandatory/rest/NoteService/notes

This is the error description I get: org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List.

The project folder:

enter image description here

Note.java:

package com.note;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "note") 

public class Note implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String title;
    private String text;

    public Note(){

    }

    public Note(int id, String title, String text){  
          this.id = id; 
          this.title = title; 
          this.text = text; 
    }


    //Getters
     public int getId() { 
          return id; 
     }  

     public String getTitle() { 
          return title; 
     } 

     public String getText() { 
          return text; 
     } 

     //Setters
     @XmlElement 
     public void setId(int id) { 
      this.id = id; 
     }

     @XmlElement 
     public void setTitle(String title) { 
      this.title = title; 
     }

     @XmlElement 
     public void setText(String text) { 
      this.text = text; 
     }   
}

NoteDataAccess.java:

public class NoteDataAccess {



    public List<Note> getAllNotes(){

        List<Note> noteList = null; 

        try {

            File file = new File("Notes.dat"); 


             if (!file.exists()) { 
                 Note note = new Note(1, "Remember this", "Buy milk and do homework");
                 noteList = new ArrayList<Note>(); 
                 noteList.add(note); 

                 saveNoteList(noteList); 
             }else{ 

                 FileInputStream fis = new FileInputStream(file);

                 ObjectInputStream ois = new ObjectInputStream(fis); 

                 noteList = (List<Note>) ois.readObject();


                 ois.close();
             } 
          } catch (IOException e) { 
             e.printStackTrace(); 
             System.out.println("NoteDataAccess_getAllNotes Exception 1");
          } catch (ClassNotFoundException e) { 
             e.printStackTrace(); 
             System.out.println("NoteDataAccess_getAllNotes Exception 2");
          }

        return noteList; 
    }



    private void saveNoteList(List<Note> noteList){
        try {

            File file = new File("Notes.dat"); 

            FileOutputStream fos;
            fos = new FileOutputStream(file); 


            ObjectOutputStream oos = new ObjectOutputStream(fos); 

            oos.writeObject(noteList);

            oos.close(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
            System.out.println("NoteDataAccess_saveNoteList Exception 1");
        } catch (IOException e) { 
            e.printStackTrace(); 
            System.out.println("NoteDataAccess_saveNoteList Exception 2");
        } 
    }

}

NoteService.java:

package com.note;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET; 
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; 
import javax.xml.bind.annotation.XmlElement;

import org.apache.tomcat.jni.User;


@Path("/NoteService") 


public class NoteService {
    NoteDataAccess theNoteDataAccess = new NoteDataAccess();

    @GET 
    @Path("/notes") 
    @Produces(MediaType.APPLICATION_XML) 
    public List<Note> getNotes(){
        return theNoteDataAccess.getAllNotes();
    }

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>NoteMandatory</display-name>
  <servlet>
    <servlet-name>Jersey RESTful 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>com.note</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey RESTful Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Jersey jar files: enter image description here

Upvotes: 0

Views: 845

Answers (1)

hack_on
hack_on

Reputation: 2520

I think the problem is that you are returning a list into an XML format. Jersey has trouble with this and you want to wrap it with a GenericEntity:

import javax.ws.rs.core.GenericEntity;

...

public class NoteService {
NoteDataAccess theNoteDataAccess = new NoteDataAccess();

@GET 
@Path("/notes") 
@Produces(MediaType.APPLICATION_XML) 
public Response getNotes(){
    List<Note> notes = theNoteDataAccess.getAllNotes();
    GenericEntity<List<Note>> entity = new GenericEntity<List<Note>>(Lists.newArrayList(notes)) {};
    return Response.ok().entity(entity).build();
}

See this SO Question.

Upvotes: 0

Related Questions