如何使用 Jersey Web 服务响应
How to Consume Jersey web service response
我使用 jax 和 java 创建了 Jersey Web 服务,当我在浏览器中粘贴 Web 服务的 url 时,它正在运行,它显示了我的数据库的 xml。
现在我想使用这个 xml ,我希望能够例如获取图像的路径并将其显示在我的主页中并获取名称和所有其他内容并制作一个instagram 提要 .
我想为 xml 中的每个对象显示图像和提要中的其他信息。
如果这个问题听起来很愚蠢,我深表歉意,但这是我第一次
我得到的 xml 是这样的对象列表:
<image>
<category>automobile</category>
<date>2019-40-08 02:40:18</date>
<description>lamborghini lp-750sv </description>
<name>New car</name>
<PATH>
C:\Users\ilyas\Desktop\PROJETS\workspace eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SupPinterest\uploads\automobilelambosv750.jpg
</PATH>
<username>ilyas110298</username>
</image>
这是我的 image.java:
package com.servlet;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
import javax.servlet.http.*;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement(name = "image")
public class image extends HttpServlet implements Serializable {
String name , description , PATH , date , category , username;
public image() {
}
public image(String name, String description, String pATH, String date, String category, String username) {
super();
this.name = name;
this.description = description;
PATH = pATH;
this.date = date;
this.category = category;
this.username = username;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getPATH() {
return PATH;
}
public String getDate() {
return date;
}
public String getCategory() {
return category;
}
public String getUsername() {
return username;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
@XmlElement
public void setDescription(String description) {
this.description = description;
}
@XmlElement
public void setPATH(String pATH) {
PATH = pATH;
}
@XmlElement
public void setDate(String date) {
this.date = date;
}
@XmlElement
public void setCategory(String category) {
this.category = category;
}
@XmlElement
public void setUsername(String username) {
this.username = username;
}
}
这是我的 imageDAO:
package com.servlet;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
public class imageDao {
public List<image> getAllImageList(){
List<image> imageList = null;
//boolean st =false;
try{
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/supdb? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","ilyas");
Statement stmt = con.createStatement();
ResultSet rs =stmt.executeQuery("select * from image");
imageList = new ArrayList<>();
while(rs.next()) {
String name = rs.getString(1);
String description = rs.getString(2);
String PATH = rs.getString(3);
String date = rs.getString(4);
String category = rs.getString(5);
String username = rs.getString(6);
imageList.add(new image (name, description, PATH, date, category, username));
}
rs.close();
stmt.close();
con.close();
return imageList;
}catch(Exception e)
{
e.printStackTrace();
}
return imageList;
}
}
最后是我的 imageSERVICE:
package com.servlet;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
@Path("/imageservice")
@XmlRootElement
public class imageService {
imageDao imageDAO = new imageDao();
@GET
@Path("/imageList")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<image> diplayImage(){
return imageDAO.getAllImageList();
}
}
我正在使用 eclipse 和 apache 并且 mysql
当我使用 API 测试仪并粘贴 api url (http://localhost:8080/SupPinterest/image/imageservice/imageList)
我收到错误
javax.servlet.ServletException:
org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError:
com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
我找到了解决办法,依赖哪里错了
here is a picture of the dependencies i put in web-inf/lib
你只需要一个一个地安装所有的依赖和相同的版本,我删除了所有的依赖然后我开始运行程序得到依赖错误并寻找jar直到它工作
我使用 jax 和 java 创建了 Jersey Web 服务,当我在浏览器中粘贴 Web 服务的 url 时,它正在运行,它显示了我的数据库的 xml。
现在我想使用这个 xml ,我希望能够例如获取图像的路径并将其显示在我的主页中并获取名称和所有其他内容并制作一个instagram 提要 .
我想为 xml 中的每个对象显示图像和提要中的其他信息。
如果这个问题听起来很愚蠢,我深表歉意,但这是我第一次
我得到的 xml 是这样的对象列表:
<image>
<category>automobile</category>
<date>2019-40-08 02:40:18</date>
<description>lamborghini lp-750sv </description>
<name>New car</name>
<PATH>
C:\Users\ilyas\Desktop\PROJETS\workspace eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SupPinterest\uploads\automobilelambosv750.jpg
</PATH>
<username>ilyas110298</username>
</image>
这是我的 image.java:
package com.servlet;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
import javax.servlet.http.*;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement(name = "image")
public class image extends HttpServlet implements Serializable {
String name , description , PATH , date , category , username;
public image() {
}
public image(String name, String description, String pATH, String date, String category, String username) {
super();
this.name = name;
this.description = description;
PATH = pATH;
this.date = date;
this.category = category;
this.username = username;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getPATH() {
return PATH;
}
public String getDate() {
return date;
}
public String getCategory() {
return category;
}
public String getUsername() {
return username;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
@XmlElement
public void setDescription(String description) {
this.description = description;
}
@XmlElement
public void setPATH(String pATH) {
PATH = pATH;
}
@XmlElement
public void setDate(String date) {
this.date = date;
}
@XmlElement
public void setCategory(String category) {
this.category = category;
}
@XmlElement
public void setUsername(String username) {
this.username = username;
}
}
这是我的 imageDAO:
package com.servlet;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
public class imageDao {
public List<image> getAllImageList(){
List<image> imageList = null;
//boolean st =false;
try{
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/supdb? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","ilyas");
Statement stmt = con.createStatement();
ResultSet rs =stmt.executeQuery("select * from image");
imageList = new ArrayList<>();
while(rs.next()) {
String name = rs.getString(1);
String description = rs.getString(2);
String PATH = rs.getString(3);
String date = rs.getString(4);
String category = rs.getString(5);
String username = rs.getString(6);
imageList.add(new image (name, description, PATH, date, category, username));
}
rs.close();
stmt.close();
con.close();
return imageList;
}catch(Exception e)
{
e.printStackTrace();
}
return imageList;
}
}
最后是我的 imageSERVICE:
package com.servlet;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
@Path("/imageservice")
@XmlRootElement
public class imageService {
imageDao imageDAO = new imageDao();
@GET
@Path("/imageList")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<image> diplayImage(){
return imageDAO.getAllImageList();
}
}
我正在使用 eclipse 和 apache 并且 mysql
当我使用 API 测试仪并粘贴 api url (http://localhost:8080/SupPinterest/image/imageservice/imageList) 我收到错误
javax.servlet.ServletException:
org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError:
com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
我找到了解决办法,依赖哪里错了
here is a picture of the dependencies i put in web-inf/lib
你只需要一个一个地安装所有的依赖和相同的版本,我删除了所有的依赖然后我开始运行程序得到依赖错误并寻找jar直到它工作