我正在尝试创建一个使用REST将数据传递给客户端的网络服务。我现在陷入了 2 个问题,一个在客户端,一个在服务器端。我一直在尝试很多事情,我比以前更加卡住了。我希望你们中的任何一个人都能帮助我。
我尝试通过浏览器和应用程序接近网络服务。如果我直接 URL 到服务器,我会收到以下错误消息:
SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.Collection<domain.Tweet>, and MIME media type text/plain was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
text/plain ->
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ReaderProvider
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
com.sun.jersey.moxy.MoxyMessageBodyWorker
com.sun.jersey.moxy.MoxyListMessageBodyWorker
如果我运行客户端应用程序,则会收到以下错误消息:
Exception in thread "AWT-EventQueue-0" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/KwetterSOAP/resources/rest/user/Hans/tweets returned a response status of 500 Internal Server Error
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:688)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:508)
at service.RestClient.getTweets(RestClient.java:52)
at kwettermonitor.KwetterFollowing.btnZoekFollowersActionPerformed(KwetterFollowing.java:103)
at kwettermonitor.KwetterFollowing.access$000(KwetterFollowing.java:20)
at kwettermonitor.KwetterFollowing$1.actionPerformed(KwetterFollowing.java:54)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
网络服务的创建方式如下:
@Path("/rest")
@Stateless
public class RESTService {
@Inject @Named(value = "kwetterService")
private KwetterService service;
@GET
@Path("/user/{user}")
@Produces(MediaType.TEXT_PLAIN)
public User getUser(@PathParam("user") String userName)
{
return service.findByName(userName);
}
@GET
@Path("/user/{user}/tweets")
@Produces(MediaType.TEXT_PLAIN)
public Collection<Tweet> getTweets(@PathParam("user") String userName)
{
System.out.println("Service: " + service);
User user = service.findByName(userName);
System.out.println(userName);
System.out.println(user);
return user.getTweets();
}
客户端如下:
public class RestClient {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "http://localhost:8080/KwetterSOAP/resources";
public RestClient() {
DefaultClientConfig config = new DefaultClientConfig();
client = Client.create(config);
webResource = client.resource(BASE_URI).path("rest");
}
public User getUser(String userName) throws UniformInterfaceException {
final WebResource.Builder userResource = webResource
.path(String.format("/user/%s", userName))
.accept(MediaType.TEXT_PLAIN) ;
return userResource.get(User.class);
}
public Collection<Tweet> getTweets(String userName) throws UniformInterfaceException {
final WebResource.Builder tweetResource = webResource
.path(String.format("/user/%s/tweets", userName))
.accept(MediaType.TEXT_PLAIN);
return tweetResource.get(new GenericType<Collection<Tweet>>(){});
}
我也有点困惑的一点是,把@XML放在哪里......附注。我已将服务注释为根元素:
@Named(value = "kwetterService")
@Stateless
@XmlRootElement
public class KwetterService {
我没有注释使用的方法:
public User findByName(String name)
{
return userDAO.getUser(name);
}
UserDAO接近数据库中的数据。我在这里没有为 REST/XML 添加任何内容。我也为用户制作了一个XMLRootElement:
@Entity
@XmlRootElement
@Table(name="Users")
public class User implements Serializable {
在方法getTweets()中,我添加了XMLElement注释:
@XmlElement
public Collection<Tweet> getTweets() {
List<Tweet> temp = new ArrayList<Tweet>(tweets);
Collections.reverse(temp);
return temp;
}
我自己的猜测是,我对方法和类的整个注释都是错误的。您能看到任何错误吗?
请记住,JAX-RS的核心是建立在HTTP之上的,HTTP不知道也不关心Java对象。从资源方法返回对象时,必须将它们转换为请求客户端可以使用的数据。此转换由名为 MessageBodyWriters 的实体完成,并且所有 JAX-RS 实现都需要附带一组预打包的实体。遗憾的是,没有要求实现能够将对象转换为TEXT_PLAIN。从您发布的堆栈跟踪中,您实际上可以看到没有能够处理该媒体类型的提供商:
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
但是,所有 JAX-RS 实现都必须提供的转换之一是从 Java 对象到 XML 的转换。因此,您可以安全地将其用作数据格式。仅显示更改,您需要合并到现有代码中:
Java Bean:
@Entity
@XmlRootElement
@Table(name="Users")
public class User implements Serializable {
@XmlWrapperElement("tweets")
public Collection<Tweet> getTweets() {
List<Tweet> temp = new ArrayList<Tweet>(tweets);
Collections.reverse(temp);
return temp;
}
}
服务器:
@Path("/rest")
@Stateless
public class RESTService {
@GET
@Path("/user/{user}")
@Produces(MediaType.APPLICATION_XML)
public User getUser(@PathParam("user") String userName);
@GET
@Path("/user/{user}/tweets")
@Produces(MediaType.APPLICATION_XML)
public Collection<Tweet> getTweets(@PathParam("user") String userName);
}
客户:
public class RestClient {
public User getUser(String userName) throws UniformInterfaceException {
final WebResource.Builder userResource = webResource
.path(String.format("/user/%s", userName))
.accept(MediaType.APPLICATION_XML) ;
return userResource.get(User.class);
}
public Collection<Tweet> getTweets(String userName)
throws UniformInterfaceException {
final WebResource.Builder tweetResource = webResource
.path(String.format("/user/%s/tweets", userName))
.accept(MediaType.APPLICATION_XML);
return tweetResource.get(new GenericType<Collection<Tweet>>(){});
}
}