在JavaFX中使用/生成HTTP RS的通用任务

时间:2016-12-09 05:54:20

标签: java multithreading generics javafx-8

所需代码

我需要找到一种通用的方式来发送带有jersey-client的http请求,而且该方法应该使用JavaFX Task来避免冻结UI。这就是我到目前为止所拥有的

通用休息服务

public class RestService<T> {

private String path;
private Class<T> entityClass;

private static final String WS_URI = "http://localhost:8080/api";

public RestService(Class<T> entityClass,String path) {
    this.path=path;
    this.entityClass = entityClass;
}

public T save(T t){
    Client client = null;
    try {
        client = ClientBuilder.newClient();
        WebTarget target = client.target(getBaseUri());
        return  (T)target.path(path).request()
                .post(Entity.entity(t, MediaType.APPLICATION_JSON), entityClass);

    } catch (RuntimeException e) {
        throw e;
    } finally { if(client != null) client.close(); }
}

public T update(T t) throws RuntimeException {
    Client client = null;
    try {
        client = ClientBuilder.newClient();
        WebTarget target = client.target(getBaseUri());
        return  (T)target.path(path).request()
                .put(Entity.entity(t, MediaType.APPLICATION_JSON), entityClass);
    } catch(RuntimeException e) {
        throw e;
    } finally { if(client != null) client.close(); }
}

public ObservableList<T> findAll(){
    Client client = null;
    try {
        client = ClientBuilder.newClient();
        WebTarget target = client.target(getBaseUri());
        return target.path(path).request()
                .get(new GenericType<ObservableList<T>>(){});
    } catch(RuntimeException e) {
        throw e;
    } finally { if(client != null) client.close(); }
}

public T delete(Integer id) throws RuntimeException {
    Client client = null;
    try {
        client = ClientBuilder.newClient();
        WebTarget target = client.target(getBaseUri());
        return (T)target.path(path+id).request().delete(entityClass);
    } catch(RuntimeException e) {
        throw e;
    } finally { if(client != null) client.close(); }
}

public T findOne(Integer id) throws RuntimeException {
    Client client = null;
    try {
        client = ClientBuilder.newClient();
        WebTarget target = client.target(getBaseUri());
        return target.path(path+id).request().get(entityClass);
    } catch(RuntimeException e) {
        throw e;
    } finally { if(client != null) client.close(); }
}


}

我无法弄清楚的是创建另一个将以通用方式调用各个方法的类。关键是为CRUD线程

中的每个JavaFX Task操作设置一个方法

1 个答案:

答案 0 :(得分:0)

怎么样

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>  
    <session-factory>  
    <property name="hbm2ddl.auto">update</property>  
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
    <property name="connection.url">jdbc:mysql://localhost:3306/shoping</property>  
    <property name="connection.username">root</property>  
    <property name="connection.password">tiger</property>  
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
    <mapping resource="product.hbm.xml"/>  
    </session-factory>  

</hibernate-configuration>