これはクライアントにList<>
のUsers
で応答しようとしているところです。私はError 500
を取得するたびに、私はちょうどString
で応答しようとしたので、サーバー/クライアントの通信に問題はありません。私はインターネットを検索し、彼らがエラーなしでLists<>
を返すいくつかの例を見つけましたが、私は仕事に就くことができません。応答する方法@GETを使用してRESTfulなサービスからリスト<>
ユーザークラス
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クラス
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のクラス
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;
}
}
クライアント
<%@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>
サーバーのスタックトレース
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)
どういうところが違うのですか?完全なスタックトレースを表示します。 –
@CássioMazzochiMolin元の投稿を編集しました – Segmentation
JSPページからではなく、PostmanのようなRESTクライアントからリクエストを実行するとどうなりますか? –