cómo escribir el servicio web Json para obtener datos correspondientes de la tabla de la base de datos en java

Este es mi codigo:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import dto.FeedObjects;


public class Project {


    public ArrayList<FeedObjects> GetFeeds(Connection connection) throws Exception
    {
        ArrayList<FeedObjects> feedData = new ArrayList<FeedObjects>();
        try
        {
            //String uname = request.getParameter("uname");
            PreparedStatement ps = connection.prepareStatement("SELECT id,title,description,url FROM website ORDER BY id DESC");
            //ps.setString(1,uname);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                FeedObjects feedObject = new FeedObjects();
                feedObject.SetId(rs.getInt("id"));
                feedObject.setTitle(rs.getString("title"));
                feedObject.setDescription(rs.getString("description"));
                feedObject.setUrl(rs.getString("url"));
                feedData.add(feedObject);
            }
            return feedData;
        }
        catch(Exception e)
        {
            throw e;
        }
    }

}

This class where are getting data of database table and converting in json format:

package webService;

import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import model.ProjectManager;

import com.google.gson.Gson;

import dto.FeedObjects;

@Path("/WebService")
public class FeedService {

    @GET
    @Path("/GetFeeds")
    @Produces("application/json")
    public String feed() {
        String feeds = null;
        try {
            ArrayList<FeedObjects> feedData = null;
            ProjectManager projectManager = new ProjectManager();
            feedData = projectManager.GetFeeds();
            Gson gson = new Gson();
            feeds = gson.toJson(feedData);

        } catch (Exception e) {
            System.out.println("error");
        }
        return feeds;
    }

}

modelo de paquete;

import java.sql.Connection;
import java.util.ArrayList;

import dao.Database;

import dao.Project;
import dto.FeedObjects;

public class ProjectManager {


public ArrayList<FeedObjects> GetFeeds(String id)throws Exception {
    ArrayList<FeedObjects> feeds = null;
    try {
            Database database= new Database();
            Connection connection = database.Get_Connection();
            Project project= new Project();
            feeds=project.GetFeeds(connection);

    } catch (Exception e) {
        throw e;
    }
    return feeds;
}

}

There is one more class where we have get set value. i am able to display all database table value in Json format Using this URL But i want when i pass id :

http://localhost:8080/RESTfulProject/REST/WebService/GetFeeds?id=1  

then it should display only id one corresponding name ,title,url. i have tried using this

http://www.9lessons.info/2012/10/restful-web-services-json-api.html example but unable to do this plz Help me

preguntado el 27 de noviembre de 13 a las 06:11

1 Respuestas

Tienes que agregar @QueryParam("id") String id en el capítulo respecto a la feed method parameter and get the value of the id and perform your operation based on the value of the id and return the JSON string.Please look below

   @GET
    @Path("/GetFeeds")
    @Produces("application/json")
    public String feed(@QueryParam("id") String id) {
        String feeds = null;
        try {
            ArrayList<FeedObjects> feedData = null;
            ProjectManager projectManager = new ProjectManager();
            // Modify the GetFeeds method by sending the value of id
            // and prepare feed data based on id
            feedData = projectManager.GetFeeds(id);
            Gson gson = new Gson();
            // create the json data for the feed data of the corresponding id value
            feeds = gson.toJson(feedData);

        } catch (Exception e) {
            System.out.println("error");
        }
        return feeds;

   }

In your GetFeeds method of ProjectManager, the GetFeeds method of Project class is invoked by passing the parameter of Connection type. Modify it to add the parameter of String type to pass the value of id from QueryParam and then modify the query as below

SELECT id,title,description,url FROM website where id = 'ID_VALUE_IN_QUERY_PARAM'

Hope this clearly explains everything. This will return the only the rows with ID value passed in query string.

respondido 27 nov., 13:07

what about feedData = projectManager.GetFeeds(id); how we can pass while here only one parameter public ArrayList<FeedObjects> GetFeeds(Connection connection) throws Exception - user2782773

the code doesn't use GetFeeds from Project class, instead from ProjectManager. you can modify the method GetFeeds with String parameter type 'id' in ProjectManager class - Keerthivasan

I have change id But there is not change in value while am calling web service like this localhost:8080/RESTfulProject/REST/WebService/GetFeeds?id=4 still all data is coming - user2782773

Can you post GetFeeds Method of ProjectManager class? Did you modify the query to add the id in where clause? - Keerthivasan

public ArrayList<FeedObjects> GetFeeds(String id)throws Exception { ArrayList<FeedObjects> feeds = null; try { Database database= new Database(); Connection connection = database.Get_Connection(); Project project= new Project(); feeds=project.GetFeeds(connection); } catch (Exception e) { throw e; } return feeds; } - user2782773

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.