我试图创建一个使用REST对数据传递到客户端的web服务。 现在我卡上的2个问题,一是在客户端,和一个在服务器端。 我已经尝试了很多东西,我得到了更卡住然后我以前。 我希望所有的你能帮助我。
我试着通过浏览器接近互联网服务和throught应用。 如果我直接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)
WebService的已创建如下:
@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;
}
我自己的猜测是,我的整个方法和类的注释是错误的。 任何错误,你可以看到什么?