Java 内部服务器错误-Tomcat&;运动衫

Java 内部服务器错误-Tomcat&;运动衫,java,rest,tomcat,jakarta-ee,jersey,Java,Rest,Tomcat,Jakarta Ee,Jersey,我正在尝试阅读以下教程,但不断出现错误: 当我尝试访问以下链接时,收到HTTP状态500-Interval Server错误: 这是我得到的错误描述: org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo严重:找不到媒体类型为application/xml的MessageBodyWriter,类型为class java.util.Array

我正在尝试阅读以下教程,但不断出现错误:

当我尝试访问以下链接时,收到HTTP状态500-Interval Server错误:

这是我得到的错误描述: org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo严重:找不到媒体类型为application/xml的MessageBodyWriter,类型为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");
        } 
    }

}
公共类NoteDataAccess{
公共列表getAllNotes(){
List noteList=null;
试一试{
文件文件=新文件(“Notes.dat”);
如果(!file.exists()){
Note Note=新注释(1,“记住这一点”,“买牛奶,做作业”);
noteList=新的ArrayList();
注释列表。添加(注释);
saveNoteList(noteList);
}否则{
FileInputStream fis=新的FileInputStream(文件);
ObjectInputStream ois=新ObjectInputStream(fis);
noteList=(List)ois.readObject();
ois.close();
} 
}捕获(IOE){
e、 printStackTrace();
System.out.println(“NoteDataAccess\u getAllNotes异常1”);
}catch(classnotfound异常){
e、 printStackTrace();
System.out.println(“NoteDataAccess\u getAllNotes异常2”);
}
返回注释列表;
}
私有void saveNoteList(列表noteList){
试一试{
文件文件=新文件(“Notes.dat”);
文件输出流;
fos=新文件输出流(文件);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
oos.writeObject(注释列表);
oos.close();
}catch(filenotfound异常){
e、 printStackTrace();
System.out.println(“NoteDataAccess\u savenotelest异常1”);
}捕获(IOE){
e、 printStackTrace();
System.out.println(“NoteDataAccess\u saveNoteList异常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();
    }

}
package.com.note;
导入java.io.IOException;
导入java.util.List;
导入javax.servlet.http.HttpServletResponse;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.FormParam;
导入javax.ws.rs.GET;
导入javax.ws.rs.PUT;
导入javax.ws.rs.Path;
导入javax.ws.rs.products;
导入javax.ws.rs.core.Context;
导入javax.ws.rs.core.MediaType;
导入javax.xml.bind.annotation.xmlement;
导入org.apache.tomcat.jni.User;
@路径(“/NoteService”)
公共类记事本服务{
NoteDataAccess theNoteDataAccess=新建NoteDataAccess();
@得到
@路径(“/notes”)
@生成(MediaType.APPLICATION\u XML)
公共列表getNotes(){
返回notedataaccess.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 RESTful应用程序
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
com.note
Jersey RESTful应用程序
/休息/*
Jersey jar文件:

我认为问题在于您将列表返回为XML格式。Jersey对此有问题,您希望用通用实体来包装它:

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();
}
import javax.ws.rs.core.GenericEntity;
...
公共类记事本服务{
NoteDataAccess theNoteDataAccess=新建NoteDataAccess();
@得到
@路径(“/notes”)
@生成(MediaType.APPLICATION\u XML)
公众回应{
List notes=notedataaccess.getAllNotes();
GenericeEntity实体=新的GenericeEntity(list.newArrayList(notes)){};
返回Response.ok().entity(entity.build();
}

请参阅。

我不这么认为。我已更新了问题并添加了jar文件的屏幕截图。可能重复的问题会按照您所说的那样得到一个错误:构造函数GenericEntity(t)不是visible@Yoseph,我现在修复了语法。语法似乎很好,但我仍然得到与以前相同的错误:严重:媒体类型未找到MessageBodyWriter type=application/xml,类型=class java.util.ArrayList,genericType=java.util.List。@Yoseph,如果您将其更改为只返回列表中的第一个音符,问题是否会消失?我是说ab在服务中退出web方法getNotes()。它以前返回过列表。作为测试,请将其更改为返回类型“Note”,然后执行return notes.get(0);(假设它有一个)。