如何使用Jersey网络服务响应

时间:2019-02-10 16:17:19

标签: rest jsp java-ee jersey

我已经使用jax和java制作了Jersey Web服务,当我将Web服务的URL粘贴到浏览器中时,它可以正常工作,它向我显示了数据库的xml。

现在,我想使用此xml,我希望能够举例说明Image的路径,并在我的主页上显示它,并获取名称和所有其他内容,并使其像instagram feed。

我想为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网址(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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,错误的依赖项

{{3}}

您只需要一个一个地安装所有依赖项并安装相同的版本,我删除了所有依赖项,然后开始运行该程序,获取依赖项错误并查找jar直到起作用为止