RESTFUL Web服务 - 不链接到Web服务的客户端应用程序

时间:2017-07-03 17:40:02

标签: java rest request client uri

如果以前曾在某处询问过,请告诉我。问题的关键可能类似,我不确定。

我基本上在Eclipse上有两个程序。一个客户端和一个带休息的Web服务(我顺便使用REST 3.1.3)。问题很简单,就是我的客户端应用程序没有链接到restful Web服务来获取我要求的请求。

即,我正在尝试从Web服务自己的数据库中获取作者频道列表。问题不在于服务本身无法检索数据,而是客户端无法访问Web服务以从中获取数据,然后显示在自己的JSP中。

以下是我尝试使用的客户端应用程序的方法:

@SuppressWarnings("unchecked")
@Override
public List<Channel> getAllChannels() {


    Client client = ClientBuilder.newClient();

    System.out.println("Client created.");

    WebTarget target = client.target("http://localhost:8080/RESTService/examples/channels/authorchannels");
    Response response = target.request().accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON).get();

    List<Channel> channels = null;

    if (response.getStatus() != 200) {

        System.out.println("Could not retrieve!" );

    } else {


        channels = (List<Channel>) response.getEntity();
        System.out.println(response.getMediaType().toString());
        System.out.println(response.readEntity(String.class));

    }

    return channels;

}

它属于以下界面和类:

类Channel.java:

package examples.pubhub.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="channels")
public class Channel implements Serializable {

@Id
@Column(name="channel_user")
private String channel_user;

@Id
@Column(name="channel_bio")
private String channel_bio;

public Channel() {


}

public Channel(String user, String bio) {
    super();
    this.channel_user = user;
    this.channel_bio = bio;

}

接口ChannelDAO.java:

package examples.pubhub.dao;

import java.util.List;

import examples.pubhub.model.Channel;

public interface ChannelDAO {

public List<Channel> getAllChannels();
public Channel getChannelByAuthorUser(String author_user);
public void createAuthorChannel(String author_user);

}

该方法将向Web服务中的以下方法发送get请求:

AuthorChannel.java(来自RESTService):

@GET
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, 
MediaType.APPLICATION_JSON})
    @Path("authorchannels")
    public List<Response> getAllChannels() {

    Session session = DAOUtilities.getSessionFactory().openSession();
    session.beginTransaction();
    Query query = session.createQuery("from Channel");

    @SuppressWarnings("unchecked")
    List<Object> channels = query.list();
    session.getTransaction().commit();

    ResponseBuilder respBuilder = null;

    if (channels == null)
        respBuilder = Response.status(Response.Status.NOT_FOUND);
    else
        respBuilder = Response.status(Response.Status.OK);

    respBuilder.entity(channels);

    System.out.println("Channels? " + channels);
    System.out.println("Users? " + ((Channel) 
    channels.get(0)).getChannel_user());
    System.out.println("Bios? " + ((Channel) 
    channels.get(0)).getChannel_bio());

    return (List<Response>) respBuilder.build();

    }

当我尝试在Postman上测试时,它给了我一个404.我不确定是什么问题。我认为URI可能不正确,但我注意到关于如何编写URI的冲突理论。

我最大的问题主要是关于这个话题的错误信息;任何帮助,将不胜感激。如果您想查看代码中的其他内容,请告诉我们。

控制器代码:

package methods;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.hibernate.Query;
import org.hibernate.Session;

import model.Channel;
import resources.DAOUtilities;

@Path("/channels")
public class AuthorChannel {


    @POST
    @Path("/{id}")
    @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void createAuthorChannel(@PathParam("id") String user, @QueryParam("bio") String bio) {

        Session session = DAOUtilities.getSessionFactory().openSession();

        Channel channel = new Channel();
        channel.setChannel_bio(bio);
        channel.setChannel_user(user);

        session.save(channel);
        session.flush();
        session.getTransaction().commit();


    }

    @GET
    @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("/authorchannels")
    public List<Response> getAllChannels() {

        Session session = DAOUtilities.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("from Channel");

        @SuppressWarnings("unchecked")
        List<Object> channels = query.list();
        session.getTransaction().commit();

        ResponseBuilder respBuilder = null;

        if (channels == null)
            respBuilder = Response.status(Response.Status.NOT_FOUND);
        else
            respBuilder = Response.status(Response.Status.OK);

        respBuilder.entity(channels);

        System.out.println("Channels? " + channels);
        System.out.println("Users? " + ((Channel) channels.get(0)).getChannel_user());
        System.out.println("Bios? " + ((Channel) channels.get(0)).getChannel_bio());

        return (List<Response>) respBuilder.build();

    }

    @GET
    @Path("/{id}")
    @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getChannelByAuthorUser(@PathParam("id") String channel_user) {

        Session session = DAOUtilities.getSessionFactory().openSession();
        Query query = session.createQuery("from Channel where channel_user = :channel_user");
        query.setParameter("channel_user", channel_user);
        Channel channel = (Channel) query.uniqueResult();
        session.getTransaction().commit();

        ResponseBuilder respBuilder = null;

        if (channel == null)
            respBuilder = Response.status(Response.Status.NOT_FOUND);
        else
            respBuilder = Response.status(Response.Status.OK);

        respBuilder.entity(channel);

        return respBuilder.build();

    }

}

0 个答案:

没有答案