内部服务器错误 - Tomcat&泽西

时间:2017-02-28 02:53:47

标签: java rest tomcat java-ee jersey

我试图通过以下教程,但我一直收到错误消息: https://www.tutorialspoint.com/restful/restful_first_application.htm

当我尝试访问以下链接时,我收到HTTP状态500 - 间隔服务器错误: http://localhost:8080/NoteMandatory/rest/NoteService/notes

这是我得到的错误说明: org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteTo SEVERE:找不到媒体类型= application / xml的MessageBodyWriter,type = class java.util.ArrayList,genericType = java.util.List。

项目文件夹:

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>

泽西jar文件: enter image description here

1 个答案:

答案 0 :(得分:0)

我认为问题在于您将列表返回到XML格式。泽西有麻烦,你想用GenericEntity包装它:

/* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'alpha',
    podModulePrefix: 'alpha/pods',
    environment: environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    }
  };

  ENV['ember-simple-auth'] = {
    authorizer: 'authorizer:session'
  };

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {

  }

  return ENV;
};

请参阅this SO Question