Reputation: 401
This is how far i have come trying to respond with a List<>
of Users
to a Client . Every time i get Error 500
so i tried to respond with just a String
and it worked so there is not a problem with the server/client communication. I searched the Internet and i found some examples that they returned Lists<>
without error but i can't get mine to work.
User Class
package org.cs131111.user;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable{
private String studentID;
private String Fname;
private String Lname;
private int semester;
public User(){}
public User(String sID,String fname,String lname,int sem){
this.studentID=sID;
this.Fname=fname;
this.Lname=lname;
this.semester = sem;
}
public String getId() {
return studentID;
}
@XmlElement
public void setId(String id) {
this.studentID = id;
}
public String getName() {
return Fname+"_"+Lname;
}
@XmlElement
public void setFName(String name) {
this.Fname = name;
}
@XmlElement
public void setLName(String name) {
this.Lname = name;
}
public int getSemester() {
return semester;
}
@XmlElement
public void setSemester(int semester) {
this.semester = semester;
}
}
UserList class
package org.cs131111.user;
import org.cs131111.db.DatabaseConnection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UserList {
public List<User> list = null;
public List<User> getAllUsers(){
User stud = null;
DatabaseConnection newc = null;
newc = new DatabaseConnection();
list = new ArrayList<User>();
try {
newc.results=newc.query.executeQuery("select * from `students`");
while(newc.results.next()){
stud = new User(newc.results.getString("studentid"),newc.results.getString("fname"),newc.results.getString("lname"),newc.results.getInt("semester"));
list.add(stud);
System.out.println(stud.getId()+" "+stud.getName());
}
} catch (SQLException ex) {
Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
}
newc.close();
return list;
}
}
UserService class
package org.cs131111.user;
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("/UserService")
public class UserService {
UserList userOb = new UserList();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
final List<User> users = userOb.getAllUsers();
return users;
}
}
The Client
<%@page import="java.util.List"%>
<%@page import="User.User"%>
<%@page import="java.io.IOException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.net.URL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
try {
URL url = new URL("http://localhost:11118/EclassServer/webresources/UserService/users");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
List<User> u= (List<User>)conn.getContent();
out.println("Output from Server .... \n");
String test=u.get(1).getId();
out.println(test);
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>
Server's stack trace
Warning: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.RuntimeException: Failed : HTTP error code : 500
at org.apache.jsp.clientGet_jsp._jspService(clientGet_jsp.java:80)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Upvotes: 0
Views: 118
Reputation: 2119
I can't post this as a comment so here it is. You can't really expect HttpUrlConnection.getContent to return a List of User instances. From the Javadoc
This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created:
If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type. If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named:
The usual way to get the content (when using HttpUrlConnection) is by using BufferedReader in combination with InputStreamReader something like
if(resCode==200){
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
stringBuilder = new StringBuilder();
String line=null;
while((line = reader.readLine()) != null){
stringBuilder.append(line + "\n");
}
}
But even now you will only get your User list as one large string (formatted as XML) and you should use an object mapper to convert the content/entity string to a List of User instances.
If, on the other hand, you used a Jersey client, it would be able to do the conversion automatically for you (it uses JAXB under the hood). Your Jersey client code might look something like (not tested)
WebTarget target = client.target(UriBuilder.fromUri("http://localhost:11118/EclassServer/webresources/UserService/users").build());
GenericType<List<User>> genType = new GenericType<List<User>>();
List<User> userList =(String) target.request().accept(MediaType.XML_APPLICATION).get(genType);
Upvotes: 1
Reputation: 38132
You don't have a correct root element. You've added the @XmlRootElement annotation to the User class, but it's not the root element as you're returning a list of users not a single user.
I recommend to specify a XSD and generate the JAXB classes rather than writing them yourself.
Upvotes: 0