【问题标题】:Internal Server Error - Tomcat & Jersey内部服务器错误 - Tomcat & Jersey
【发布时间】:2017-02-28 02:53:46
【问题描述】:

我正在尝试完成以下教程,但我不断收到错误消息: 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: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List。

项目文件夹:

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 文件:

【问题讨论】:

标签: java rest tomcat jakarta-ee jersey


【解决方案1】:

我认为问题在于您将列表返回为 XML 格式。 Jersey 遇到了麻烦,你想用 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();
}

this SO Question

【讨论】:

  • 按你说的做会报错:构造函数 GenericEntity>(T) is not visible
  • @Yoseph,我现在修正了语法。
  • 语法看起来不错,但我仍然得到与以前相同的错误:严重:找不到媒体类型=应用程序/xml,类型=类 java.util.ArrayList,通用类型=java.util 的 MessageBodyWriter .List.
  • @Yoseph,如果您将其更改为仅返回列表中的第一个音符,问题会消失吗?
  • 我说的是您服务中的网络方法 getNotes()。它之前返回 List 。作为测试,将其更改为返回类型“Note”并返回 notes.get(0); (假设它有一个)。
猜你喜欢
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2018-02-04
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多