hibernate - JSON unmarshalling to POJO and inserting -
i unmarshal json string pojo class. reading existing url: https://builds.apache.org/job/accumulo-1.5/api/json
i using apache camel unmarshal url
@component public class routebuilder extends springroutebuilder { private logger logger = loggerfactory.getlogger(routebuilder.class); @override public void configure() throws exception { logger.info("configuring route"); //properties die hij niet vindt in de klasse negeren objectmapper objectmapper = new objectmapper(); objectmapper.configure(deserializationconfig.feature.fail_on_unknown_properties, false); dataformat reportformat = new jacksondataformat(objectmapper, healthreport.class); from("timer://foo?fixedrate=true&delay=0&period=2000&repeatcount=1") .routeid("accumolotojsonroute") .setheader(exchange.http_method, constant("get")) .to("https://builds.apache.org:443/job/accumulo-1.5/api/json") .convertbodyto(string.class) .unmarshal(reportformat) //instance van build .log(logginglevel.debug, "be.kdg.teamf", "project: ${body}") .to("hibernate:be.kdg.teamf.model.healthreport"); } } so far good. insert 'healthreport' node using hibernate annotations.
@xmlrootelement(name = "healthreport") @jsonrootname(value = "healthreport") @entity(name = "healthreport") public class healthreport implements serializable { @id @generatedvalue(strategy = generationtype.identity) private int id; @column @jsonproperty("description") private string description; @column @jsonproperty("iconurl") private string iconurl; @column @jsonproperty("score") private int score; public healthreport() { } public healthreport(int score, string iconurl, string description) { this.score = score; this.iconurl = iconurl; this.description = description; } public string getdescription() { return description; } public string geticonurl() { return iconurl; } public int getid() { return id; } public int getscore() { return score; } public void setdescription(string description) { this.description = description; } public void seticonurl(string iconurl) { this.iconurl = iconurl; } public void setid(int id) { id = id; } public void setscore(int score) { this.score = score; } } this problem is. not recognize annotations , null values inserted in database
@xmlrootelement(name = "healthreport") @jsonrootname(value = "healthreport") does know how fix this?
thanks
fixed using processor route
public class healthreportprocessor implements processor { @autowired private configurationservice configurationservice; @override public void process(exchange exchange) throws exception { objectmapper mapper = new objectmapper(); jsonnode root = mapper.readtree(exchange.getin().getbody().tostring()); arraynode report = (arraynode) root.get("healthreport"); int configid = configurationservice.findjenkinsconfigurationbyname(root.get("displayname").astext()).getid(); (jsonnode node : report) { jsonobject obj = new jsonobject(); obj.addproperty("description", node.get("description").astext()); obj.addproperty("iconurl", node.get("iconurl").astext()); obj.addproperty("score", node.get("score").asint()); obj.addproperty("jenkinsconfig", configid); exchange.getin().setbody(obj.tostring()); } } } it working think there better solution. if have better solution please let me know ;)
Comments
Post a Comment