hi,
Thanks for your reply.
*I just defined a pojo bean:*
package igniteproject;
import java.util.ArrayList;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
public class DataBean {
private String s1;
private String s2;
public DataBean(String s1,String s2) {
this.s1=s1;
this.s2=s2;
}
public void setS1(String s1) {
this.s1 = s1;
}
public String getS1() {
return s1;
}
public void setS2(String s2) {
this.s2 = s2;
}
public String getS2() {
return s2;
}
public String toString(){
String s="";
s = JSONObject.fromObject(DataBean.this).toString();
return s;
}
}
*and my cache config xml:*
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean id="grid.cfg"
class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="userAttributes">
<map>
<entry key="ROLE" value="worker"/>
</map>
</property>
<property name="cacheConfiguration">
<list>
<bean
class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="test"/>
<property name="cacheMode" value="PARTITIONED"/>
</bean>
</list>
</property>
<property name="discoverySpi">
<bean
class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<bean
class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
<property name="addresses">
<list>
<value>127.0.0.1</value>
<value>127.0.0.1:47500..47509</value>
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
*and my cache bean:*
package igniteproject;
import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
public class CacheServerBean extends Thread{
public static void main(String[] args) {
try (Ignite ignite = Ignition.start("mycache.xml")) {
IgniteCache<String, DataBean> cache =
ignite.getOrCreateCache("myCacheName");
for (int i = 0; i < 2; i++)
cache.put(i+"", new DataBean("s1-"+i,"s2-"+i));
while(true){
for (Cache.Entry<String, DataBean> data : cache){
System.out.println(">>> list cache key=: " +
data.getKey());
DataBean d = data.getValue();
System.out.println(">>> list cache value=: " +
d.getS1()+" and "+d.getS2());
}
sleep(1000*10);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
*I add two DataBean instances to cache,the result is right:*
>>> list cache key=: 0
>>> list cache value=: s1-0 and s2-0
>>> list cache key=: 1
>>> list cache value=: s1-1 and s2-1
*I get the cache by rest api,the result is right:*
http://localhost:8080/ignite?cmd=get&key=0&cacheName=myCacheName
{"affinityNodeId":"42b4e603-8512-440b-b148-c881816ecafc","error":"","response":{"s1":"s1-0","s2":"s2-0"},"sessionToken":"","successStatus":0}
*Then I want to put a DataBean instance to the cache by rest api,the cache
key is 9,the rest response result is right:*
http://localhost:8080/ignite?cmd=put&key=9&val={"s1":"s1-9","s2":"s2-9"}&cacheName=myCacheName
{"affinityNodeId":"42b4e603-8512-440b-b148-c881816ecafc","error":"","response":true,"sessionToken":"","successStatus":0}
*But actually, I got String value in my cache when I get the cache value put
by rest api.
When " DataBean d = data.getValue(); " ,there is a error.
*
>>> list cache key=: 9
java.lang.ClassCastException: java.lang.String cannot be cast to
igniteproject.DataBean
at igniteproject.CacheServerBean.main(CacheServerBean.java:26)
Thanks!
Tony
--
View this message in context:
http://apache-ignite-users.70518.x6.nabble.com/How-to-put-a-POJO-bean-value-to-cache-by-rest-api-tp3639p3647.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.