java - 内部服务器错误 - Tomcat 和 Jersey

标签 java rest tomcat jakarta-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:MessageBodyWriter 找不到媒体类型=application/xml,类型=class java.util.ArrayList,genericType=java.util.List。

项目文件夹:

enter image description here

笔记.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");
        } 
    }

}

注意服务.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 文件: enter image description here

最佳答案

我认为问题在于您将列表返回为 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 .

关于java - 内部服务器错误 - Tomcat 和 Jersey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499321/

相关文章:

jsp - tomcat如何以及在哪里存储在计算机中的dotCMS上创建的文件?

java - 我在哪里可以看到 Sun JDK 的源代码?

java - 在 ResourceContext initResource 创建的 JAX-RS 子资源实例中不可能进行 CDI 注入(inject)

javascript - 从react.js 中的Express.js API 获取数据。 postman 的请求正在运行

java - 更改用户后 Hibernate 不运行

java - Tomcat:每个应用程序请求的状态代码 404

java - 如何在JETTY服务器上部署war文件

java - Maven 共享依赖

http - PUT URL 是否真的需要指定资源

java - 是否可以以未定义的顺序检索路径参数?