gpt4 book ai didi

java - REST + Java + 状态 500 内部错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:43:14 25 4
gpt4 key购买 nike

好的,我正在使用 Java 编写一个 REST 服务器并尝试对其进行测试,但我不断收到错误代码 500 请帮忙。我已经逐步完成了代码,并且知道它到达了此方法的底部,其中包含格式正确的 DataClass 对象的 ArrayList(我已经检查过它们并在没有 REST 前端的情况下成功运行)。

这是我正在调用的 REST 方法

@GET
@Produces(MediaType.APPLICATION_XML)
public List<DataClass> receiveXML(
@DefaultValue("null") @QueryParam("artist") String artist,
@DefaultValue("null") @QueryParam("title") String title,
@DefaultValue("null") @QueryParam("album") String album,
@DefaultValue("null") @QueryParam("genre") String genre,
@DefaultValue("null") @QueryParam("type") String type,
@DefaultValue("false") @QueryParam("open") Boolean open,
@DefaultValue("false") @QueryParam("close") Boolean close,
@DefaultValue("noPath") @QueryParam("path") String path,
@DefaultValue("noKey") @QueryParam("key") String key,
@DefaultValue("null") @QueryParam("show") String show,
@DefaultValue("null") @QueryParam("season") String season,
@DefaultValue("null") @QueryParam("format") String format)
{

if(!artist.equals("null"))
this.artist = artist;
if(!title.equals("null"))
this.title = title;
if(!album.equals("null"))
this.album = album;
if(!genre.equals("null"))
this.genre = genre;
if(!type.equals("null"))
this.type = type;
if(!open.equals("false"))
this.open = open;
if(!close.equals("false"))
this.close = close;
if(!path.equals("noPath"))
this.path = path;
if(!key.equals("noKey"))
this.keyword = key;
if(!show.equals("null"))
this.show = show;
if(!season.equals("null"))
this.season = season;
if(!format.equals("null"))
this.format = format;

MultivaluedMap<String,String> queryParams = buildMap();
List<DataClass> resp = receive(queryParams);
return resp;
}

这是数据类

@XmlRootElement
public class DataClass {
public String pathname;
ArrayList<String> parsedSet;
ResultSet resultSet;
public String id, path, type, category, size, update, idMeta, title, description;
public String genre, album, artist, show, season;

public DataClass(ResultSet resultSet){
this.resultSet = resultSet;
parsedSet = new ArrayList<String>();
setStringVariables();
}
/**
* Sets the pathname variable of the local file to be returned
* @param pathname
*/
public DataClass(String pathname){
this.pathname = pathname;
}


methods to set the fields...

}

这是我调用服务器的方式,我知道服务器被正确调用,因为我有一个只返回字符串的测试方法。

public static void testXML() {
//a map that adds parameters that will then be added to the the WebService
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("type", "music");
//retrieve info for an artist in XML format
System.out.println(service.path("rest").path("media").queryParams(queryParams).accept(
MediaType.APPLICATION_XML).get(String.class));
}

这是我的错误

Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/TigrisRESTServer/rest/media?type=music returned a response status of 500 Internal Server Error
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:676)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)
at tigris.restserver.client.ourClient.testXML(ourClient.java:45)
at tigris.restserver.client.ourClient.main(ourClient.java:28)

对不起,我忘了包含一些东西

定义receiveXML的类有这个

@Path("/media")
public class Server {
}

这是 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RestServer</display-name>
<servlet>
<servlet-name>Tigris REST Server App</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>tigris.restserver.connection</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Tigris REST Server App</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

这工作正常(它所做的只是返回一个字符串)

这是客户端

public static void testTEST() {
System.out.println(service.path("rest").path("media").accept(
MediaType.TEXT_PLAIN).get(String.class));
}

这是服务器端

@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Server";
}

谢谢twain249

最佳答案

好像你忘了包含它应该映射的路径。

@Path("/rest/")

GET http://localhost:8080/TigrisRESTServer/rest/media?type=music

如果你想映射到 URL/rest/就是。

例如,请参阅此链接 Jersey Documentation

关于java - REST + Java + 状态 500 内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com