I have a custom java class which has an ArrayList and corresponding to it have a custom vo class which has ArrayCollection (in Flex).
I want to return data from Java to flex. Every variable in java is getting mapped to vo perfectly except for ArrayList.
When trying to retrive
When trying to retrive exposureUSDList (which is an arrayList) in flex I am getting empty ArrayCollection. It is not getting mapped properly
Please find my code below:
Flex Code:
package com.example.vo
{
import com.adobe.cairngorm.vo.IValueObject;
import mx.collections.ArrayCollection;
[Bindable]
[RemoteClass(alias="com.example.vo.Summary")]
public class Summary
{
public var productId : String;
public var clientId : String;
public var brokerId : String;
public var acctNo : Number;
public var exposureUSDList:ArrayCollection=new ArrayCollection();
}
}
Java code:
package com.example.vo;
import java.io.Serializable;
import java.util.ArrayList;
public class Summary implements Serializable {
static final long serialVersionUID = -1L;
private String productId ;
private String clientId ;
private String brokerId ;
private String acctNo ;
private ArrayList exposureUSDList= new ArrayList();
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getBrokerId() {
return brokerId;
}
public void setBrokerId(String brokerId) {
this.brokerId = brokerId;
}
public int getAcctNo() {
return acctNo;
}
public void setAcctNo(int acctNo) {
this.acctNo = acctNo;
public ArrayList getExposureUSDList() {
return exposureUSDList;
}
public void setExposureUSDList(double exposureUSD) {
this.exposureUSDList.add(exposureUSD);
}
}
Please Help!!
I think the reason could be that you're passing double to the array list setter. Try pass ArrayList.
Related
I want to indicate the pathvariable secondpart. Thats not possible because an errormessage shows: Could not determine type for: veranstaltung.Identificationnumber, at table: teilnehmer, for columns: [org.hibernate.mapping.Column(id)]
What I have to change to use the variable secondpart? How can I access the variable secondpart in the method of the Controller?
package veranstaltung;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Teilnehmer {
static int idnumber=69;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Identificationnumber id;
private String name;
private String vorname;
private String wohnort;
protected Teilnehmer() {}
public Teilnehmer(Identificationnumber id, String name, String vorname,String wohnort)
{
this.id=id;
this.name=name;
this.vorname=vorname;
this.wohnort=wohnort;
}
public static String erzeugeID ()
{
String id= "JAVALAND-";
id=id+idnumber;
idnumber++;
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getWohnort() {
return wohnort;
}
public void setWohnort(String wohnort) {
this.wohnort = wohnort;
}
#Override
public String toString()
{
return id.getfullID()+" "+getName()+" "+getVorname()+" "+getWohnort();
}
}
package veranstaltung;
public class Identificationnumber {
private String firstpart;
private Long secondpart;
public Identificationnumber(String firstpart, Long secondpart)
{
this.firstpart=firstpart;
this.secondpart=secondpart;
}
public String getFirstpart() {
return firstpart;
}
public void setFirstpart(String firstpart) {
this.firstpart = firstpart;
}
public Long getSecondpart() {
return secondpart;
}
public void setSecondpart(Long secondpart) {
this.secondpart = secondpart;
}
public String getfullID()
{
return firstpart+' '+secondpart;
}
}
package veranstaltung;
import veranstaltung.Teilnehmer;
import veranstaltung.Identificationnumber;
import veranstaltung.TeilnehmerRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
#RestController
public class TeilnehmerController {
#Autowired
TeilnehmerRepository teilnehmerRepository;
#GetMapping("/teilnehmer")
Iterable<Teilnehmer> teilnehmer(){
return this.teilnehmerRepository.findAll();
}
#GetMapping("/teilnehmer/{id}")
Teilnehmer teilnehmerById(#PathVariable Long secondpart){
Optional<Teilnehmer> teilnehmerOptional = this.teilnehmerRepository.findById(secondpart);
if(teilnehmerOptional.isPresent()) {
return teilnehmerOptional.get();
}
return null;
}
}
Your Identificationnumber is not primitive but custom class. What you are doing is trying to use Long against your Identificationnumber in teilnehmerById method.
I would suggest to either change #Id column of Teilnehmer to Long from Identificationnumber or you can still pass Identificationnumber in teilnehmerById method by doing something like below (This is based on consideration that you have implemented your own findById which will convert Identificationnumber to Long):
#GetMapping("/teilnehmer/{id}")
Teilnehmer teilnehmerById(#PathVariable Long secondpart){
Identificationnumber number = new Identificationnumber();
number.setSecondpart(secondpart);
Optional<Teilnehmer> teilnehmerOptional = this.teilnehmerRepository.findById(number.getfullID());
if(teilnehmerOptional.isPresent()) {
return teilnehmerOptional.get();
}
return null;
Based on hibernate error, it looks like you have to use my first option which is change #Id of Teilnehmer to Long.
Edit : just updated code so that you can use composition of String to Long.
I'm trying to get the parameters inside jSon items>item>image>images>transparent using the Gson library. The idea is to capture transparent, transparent_blank and transparent_dark. But I don't know how I can get these values, for the moment I have created the following:
Json
{
"date_layout":"day-month-year",
"lastupdate":1547596830,
"items":[{
"name":"Cleans Cuts",
"featured":"true",
"item":{
"image":"http:www.domain.com/unwanted_image.jpg",
"images":{
"transparent":"http:www.domain.com/desired_image1.jpg",
"transparent_blank":"http:www.domain.com/desired_image2.jpg",
"transparent_dark":"http:www.domain.com/desired_image3.jpg"
}
}
},
{
"name":"Cleans Cuts",
"featured":"true",
"item":{
"image":"http:www.domain.com/unwanted_image.jpg",
"images":{
"transparent":"http:www.domain.com/desired_image1.jpg",
"transparent_blank":"http:www.domain.com/desired_image2.jpg",
"transparent_dark":"http:www.domain.com/desired_image3.jpg"
}
}
}]
}
.MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.example.com/file.json";
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
String response_jSon = response.body().string();
Gson gson = new Gson();
Datos datosFinal = gson.fromJson(response_jSon, Datos.class);
for (int i=0; i<datosFinal.items.size(); i++){
Log.d("msg_2", datosFinal.items.get(i).name);
}
}
And I'm only able to get to items as you can see in the .MainActivity Log.
Class Objects for the Json
Datos.java
public class Datos {
public String date_layout;
public Int lastupdate;
List<items> items;
}
items.java
public class items {
public String name;
}
The json is not very clean, I would suggest that you rework how this json string is generated, but if you still want to achieve getting those values without reworking the json, you need to change your Items class to :
`public class items {
public String name;
public String featured;
public Item item;
}`
Then you need to create another class called Item
`public class Item{
public String image;
public Image images;
}`
You will also need to create an Image class, like so:
`public class Image {
public String transparent;
public String transparent_blank;
public String transparent_dark;
}`
Then you can log these values in your loop:
`Datos datosFinal = gson.fromJson(response_jSon, Datos.class);
for (int i=0; i<datosFinal.items.size(); i++){
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent);
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_blank);
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_dark);
}`
While this may work, I would highly suggest you to rework your json and make it easier to maintain.
You have to create the following class also to getting value for transparent, transparent_blank and transparent_dark
Images.java
Item.java
Replace this class:
public class Items{
private Item item;
private String name;
private String featured;
public Item getItem ()
{
return item;
}
public void setItem (Item item)
{
this.item = item;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public String getFeatured ()
{
return featured;
}
public void setFeatured (String featured)
{
this.featured = featured;
}
}
Add this class:
public class Images{
private String transparent_blank;
private String transparent_dark;
private String transparent;
public String getTransparent_blank ()
{
return transparent_blank;
}
public void setTransparent_blank (String transparent_blank)
{
this.transparent_blank = transparent_blank;
}
public String getTransparent_dark ()
{
return transparent_dark;
}
public void setTransparent_dark (String transparent_dark)
{
this.transparent_dark = transparent_dark;
}
public String getTransparent ()
{
return transparent;
}
public void setTransparent (String transparent)
{
this.transparent = transparent;
}
}
Also, Add this Class:
public class Item {
private Images images;
private String image;
public Images getImages ()
{
return images;
}
public void setImages (Images images)
{
this.images = images;
}
public String getImage ()
{
return image;
}
public void setImage (String image)
{
this.image = image;
}
}
Now, you will get the value using the getter method.
you must use
public class items {
public String name;
#Expose(serialize = false, deserialize = false)
public String featured;
#Expose(serialize = false, deserialize = false)
public item mItem;
}
public class item {
#Expose(serialize = false, deserialize = false)
public String image;
#Expose(serialize = false, deserialize = false)
public images mImages;
}
public class images {
#Expose(serialize = false, deserialize = false)
public String transparent;
#Expose(serialize = false, deserialize = false)
public String transparent_blank;
#Expose(serialize = false, deserialize = false)
public String transparent_dark;
}
you can genetate pojo by using http://www.jsonschema2pojo.org/
JSON String
{
"order":{
"address":{
"city":"seattle"
},
"orderItem":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
}
Order Class
public class Order {
private OrderItem[] orderItems;
private CustomerAddress address;
Order(OrderItem[] orderItems, CustomerAddress address ) {
this.orderItems = orderItems;
this.address = address;
}
public OrderItem[] getOrderItems() {
return orderItems;
}
public void setOrderItems(OrderItem[] orderItems) {
this.orderItems = orderItems;
}
public CustomerAddress getAddress() {
return address;
}
public void setAddress(CustomerAddress address) {
this.address = address;
}
}
My OrderItem class
package com.cbd.backend.model;
import org.springframework.data.annotation.Id;
public class OrderItem {
#Id
private String id;
private String itemId;
private String count;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
unit Test that blows up
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
}
Unit test to demonstrate issue
package com.cbd.backend.model;
import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.*;
public class OrderTest {
Gson gson = new Gson();
#Test
public void gsonToOrder() {
Order order = gson.fromJson( a, Order.class );
assertNotNull(order);
assertNotNull(order.getOrderItems()[0]);
}
private final String a = "{ \"order\": { \"address\": { \"city\": \"seattle\" },\"orderItem\":[{ \"itemId\":\"lkasj\", \"count\":2 }, { \"itemId\":\"ldka\", \"count\":3 } ] } }";
}
Should I be using something other than gson or am i constructing this incorrectly
There are two problems in your code:
The root element of your JSON is "order", but the class does not have a property with this name. Try changing you model or just removing the element from the JSON.
There is a mismatch in the name of the "orderItem" property. It is plural in the class, but singular in the JSON.
To sum it up, the following JSON will work without any changes to the code.
{
"address":{
"city":"seattle"
},
"orderItems":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
Also, "count" as it appears in the JSON seems to be numeric, so you might want to change the type of OrderItem.count to int or java.lang.Integer.
I need my server to unmarshal xml files. I have implemented the class to do this on my own computer running Java 1.8 and it works perfectly. However when I run the exact same code on the server, which runs Java 1.7, the object is created but I get a null value for the 'domain' element.
My code may not be ideal practice when using JAXB, but as I said on java 1.8 it does exactly what I need it to. Does anybody have an idea as to the cause of this behaviour? Any help would be greatly appreciated.
This is an example xml file which I am trying to unmarshal:
<planning:metadata xmlns:planning="http://planning.com">
<domain id=numeric">
<title xml:lang="en">The numeric formulation</title>
<files_last_modified>2002-06-01T12:00:00</files_last_modified>
<metadata_last_modified>2014-04-02T11:31:17.471631</metadata_last_modified>
<published>2002-06-01T12:00:00</published>
<link>http://ipc.icaps.org/</link>
<requirements>
<typing/>
<fluents/>
</requirements>
<problems>
<problem domain_file="domain.pddl" number="1" problem_file="p01.pddl"/>
<problem domain_file="domain.pddl" number="2" problem_file="p02.pddl"/>
<problem domain_file="domain.pddl" number="3" problem_file="p03.pddl"/>
<problem domain_file="domain.pddl" number="4" problem_file="p04.pddl"/>
<problem domain_file="domain.pddl" number="5" problem_file="p05.pddl"/>
<problem domain_file="domain.pddl" number="6" problem_file="p06.pddl"/>
</problems>
</domain>
</planning:metadata>
This is the java file:
package server;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
#XmlRootElement(name="metadata")
public class XmlDomain {
private XmlDomain.Domain domain;
public XmlDomain.Domain getDomain() {
return domain;
}
#XmlElement
public void setDomain(XmlDomain.Domain domain) {
this.domain = domain;
}
public static class Domain {
private String id;
private String title;
private Date filesModifiedDate;
private Date metaModifiedDate;
private Date publishedDate;
private String link;
private Requirements requirements;
private Problems problems;
public String getId() {
return id;
}
#XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
#XmlElement
public void setTitle(String title) {
this.title = title;
}
public Date getFiles_last_modified() {
return filesModifiedDate;
}
#XmlElement
public void setFiles_last_modified(Date date) {
filesModifiedDate = date;
}
public Date getMetadata_last_modified() {
return metaModifiedDate;
}
#XmlElement
public void setMetadata_last_modified(Date date) {
metaModifiedDate = date;
}
public Date getPublished() {
return publishedDate;
}
#XmlElement
public void setPublished(Date date) {
publishedDate = date;
}
public String getLink() {
return link;
}
#XmlElement
public void setLink(String link) {
this.link = link;
}
public Requirements getRequirements() {
return requirements;
}
#XmlElement
public void setRequirements(Requirements requirements) {
this.requirements = requirements;
}
public Problems getProblems() {
return problems;
}
#XmlElement
public void setProblems(Problems problems) {
this.problems = problems;
}
public static class Requirements {
// Strings representing domain requirements
private String strips = null;
private String typing = null;
private String durative = null; // durative-actions
private String fluents = null;
private String timed = null; // time-initial-literal
private String equality = null;
private String inequalities = null; // duration_inequalities
public String getStrips() {
return strips;
}
#XmlElement(name = "strips")
public void setStrips(String strips) {
this.strips = strips;
}
public String getTyping() {
return typing;
}
#XmlElement(name = "typing")
public void setTyping(String typing) {
this.typing = typing;
}
public String getDurative() {
return durative;
}
#XmlElement(name = "durative-actions")
public void setDurative(String durative) {
this.durative = durative;
}
public String getFluents() {
return fluents;
}
#XmlElement(name = "fluents")
public void setFluents(String fluents) {
this.fluents = fluents;
}
public String getTimed() {
return timed;
}
#XmlElement(name = "timed-initial-literals")
public void setTimed(String timed) {
this.timed = timed;
}
public String getEquality() {
return equality;
}
#XmlElement(name = "equality")
public void setEquality(String equality) {
this.equality = equality;
}
public String getInequalities() {
return inequalities;
}
#XmlElement(name = "duration_inequalities")
public void setInequalities(String inequalities) {
this.inequalities = inequalities;
}
}
public static class Problems {
private ArrayList<Problem> problems;
public ArrayList<Problem> getProblem() {
return problems;
}
public void setProblem(ArrayList<Problem> problems) {
this.problems = problems;
}
public static class Problem {
private String domainFile;
private int number;
private String problemFile;
public String getDomain_file() {
return domainFile;
}
#XmlAttribute(name = "domain_file")
public void setDomain_file(String domainFile) {
this.domainFile = domainFile;
}
public int getNumber() {
return number;
}
#XmlAttribute
public void setNumber(int number) {
this.number = number;
}
public String getProblem_file() {
return problemFile;
}
#XmlAttribute(name = "problem_file")
public void setProblem_file(String problemFile) {
this.problemFile = problemFile;
}
public void addResult(Planner planner, double result) {
resultMap.put(planner, result);
}
public double getResult(Planner planner) {
return resultMap.get(planner);
}
public HashMap<Planner, Double> getResultMap() {
return resultMap;
}
}
}
}
}
And the package file:
#XmlSchema(
namespace = "http://planning.com",
elementFormDefault = XmlNsForm.QUALIFIED)
package server;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Your XML is incorrect according to your schema as defined by your java objects. Everything in your schema is under the http://planning.com namespace but in your XML the only element defined in that namespace is metadata. You need either:
Set http://planning.com to the default namespace.
<metadata xmlns="http://planning.com">
<domain id=numeric">
...
Use the prefix on all the elements
<planning:metadata xmlns:planning="http://planning.com">
<planning:domain id=numeric">
...
Alternativly if the XML can be assumed to be correct but your Java classes are incorrect. In this case just remove the #XmlSchema annotation from your package-info.java and make your Java read:
#XmlRootElement(name="metadata", namespace="http://planning.com")
public class XmlDomain {
...
You dont really have a question in your post as a commentered mentioned so I am unsure if your asking why this works in 1.8 and not in 1.7 and are aware that the XML is incorrect or just didnt know you have some bad XML.
As to why this works in 1.8 is still a mystery to me. I will do a little bit more poking at it and try to find out why out of my own curiosity.
I am using XStream Library.
Link of xml service
http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=ttc&r=54
My Classes......
package com.example.myjakcontest;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
public class Body {
#XStreamAlias("copyright")
private String _copyright;
private Route route;
public String get_copyright() {
return this._copyright;
}
public void set_copyright(String _copyright) {
this._copyright = _copyright;
}
public Route getRoute() {
return this.route;
}
public void setRoute(Route route) {
this.route = route;
}
}
package com.example.my**jakcontest;**
import java.util.List;
public class Direction{
private String _branch;
private String _name;
private String _tag;
private String _title;
private String _useForUI;
private List<Stop> stop;
public String get_branch(){
return this._branch;
}
public void set_branch(String _branch){
this._branch = _branch;
}
public String get_name(){
return this._name;
}
public void set_name(String _name){
this._name = _name;
}
public String get_tag(){
return this._tag;
}
public void set_tag(String _tag){
this._tag = _tag;
}
public String get_title(){
return this._title;
}
public void set_title(String _title){
this._title = _title;
}
public String get_useForUI(){
return this._useForUI;
}
public void set_useForUI(String _useForUI){
this._useForUI = _useForUI;
}
public List<Stop> getStop(){
return this.stop;
}
public void setStop(List<Stop> stop){
this.stop = stop;
}
}
Async Task
XStream x = new XStream();
x.alias("body", Body.class);
x.alias("stop", Stop.class);
x.alias("route", Route.class);
x.alias("direction", Direction.class);
x.alias("path", Path.class);
x.alias("point", Point.class);
x.addImplicitCollection(Route.class, "stop");
x.addImplicitCollection(Route.class, "direction");
x.addImplicitCollection(Route.class, "path");
x.addImplicitCollection(Direction.class, "stop");
x.addImplicitCollection(Path.class, "point");
Body object = (Body) x.fromXML(httpResponse.getEntity()
.getContent());
// Function converts XML to String
String xml = convertStreamToString(httpResponse.getEntity()
.getContent());
Body b = (Body) x.fromXML(xml);
I have all the classes but in object "b" i am getting null.
Try JAXB .It s a standard way of doing it...!
refer the link www.javatpoint.com/jaxb-unmarshalling-example