我需要找到一种通用的方式来发送带有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
操作设置一个方法
答案 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>