我正在尝试使用JAX-RS学习RESTful Web服务的实现。
我有一个名为MovieObject
的课程,其中包含有关电影的基本信息。一个名为ResourceHandler
的类,负责处理请求并发送响应。
以下是我的 MovieObject.java
package org.auro.self.movielib.resource;
public class MovieObject {
private String movieName;
private String yearOfRelease;
private int movieRating;
public MovieObject(){
}
public MovieObject(String movieName,
String yearOfRelease,
int movieRating){
this.movieName = movieName;
this.yearOfRelease = yearOfRelease;
this.movieRating = movieRating;
}
public String getMovieName(){
return movieName;
}
public String getYearOfRelease(){
return yearOfRelease;
}
public int getMovieRating(){
return movieRating;
}
}
以下是我的 ResourceHandler.java
package org.auro.self.movielib.resource;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/message")
public class ResourceHandler {
@GET
@Produces(MediaType.TEXT_PLAIN)
public List<MovieObject> getListOfMovies(){
MovieObject mv1 = new MovieObject("The Independence Day 1", "1996", 7);
MovieObject mv2 = new MovieObject("The Independence Day 2", "2016", 4);
List<MovieObject> listOfMovies = new ArrayList<MovieObject>();
listOfMovies.add(mv1);
listOfMovies.add(mv2);
return listOfMovies;
}
}
以下是我的 index.jsp
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/myresource">Jersey resource</a>
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
for more information on Jersey!
<p> Get Movies <a href="webapi/message"> here</a>
</body>
</html>
最后, web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web 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>org.auro.self.movielib</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
如果您查看我的 index.jsp ,您会注意到<p> Get Movies <a href="webapi/message"> here</a>
的任何请求实际上由 ResourceHandler.java
对该类的GET
请求应该以纯文本形式返回电影列表,但我最终得到HTTP Status 500 - Internal Server Error
这就是我在控制台中获得的内容
INFO: Server startup in 2430 ms
Aug 20, 2016 11:52:58 PM 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<org.auro.self.movielib.resource.MovieObject>.
我是否遗漏了导致此错误的任何内容?
答案 0 :(得分:0)
这是内容协商问题(misscommunication)。
您应该更改应用程序以返回xml而不是station_cfg={}
station_cfg.ssid=SSID
station_cfg.pwd=PASSWORD
wifi.sta.config(station_cfg)
:
TEXT_PLAIN
或使用包含 @Produces({ MediaType.APPLICATION_XML })
标头的请求调用您的服务(默认情况下为Accept: text/plain
或类似内容)
您还可以尝试基于网址的内容协商,但这只是AFAIK Resteasy功能:
https://docs.jboss.org/resteasy/docs/3.0.6.Final/userguide/html/JAX-RS_Content_Negotiation.html