Convert csv to xml using bindy - apache-camel

I have csv like this:
31 name 2018-11-01 2018-10-31 12:05:16 3
1 10 1.00 13.00 14
2 20 1.00 14.00 14
3 30 1.00 15.00 14
fields in one line are separated by tab
and next I have another lines, separated tab too. Every line is separated by '\n'
How should me classes looks like?
I did something like this:
public class Order {
private int id;
private String name;
private String date1;
private int date2;
private char numerOfFile;
private List<OrderBody> orderBodyList;
}
public class OrderBody {
private int lp;
private int id;
private BigDecimal priceByOne;
private BigDecimal count;
private int foreignId;
}

This is actually possible with camel-bindy using FixedLengthRecord model with tab as delimiter. Last field in the model seems to need a length but the others can use tab as a delimiter....
Here's an example code:
OrderHeader:
import java.util.Date;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
#FixedLengthRecord()
public class OrderHeader {
#DataField(pos = 1, delimiter = "\t")
private int id;
#DataField(pos = 2, delimiter = "\t")
private String name;
#DataField(pos = 3, delimiter = "\t", pattern = "yyyy-MM-dd")
private Date date1;
#DataField(pos = 4, delimiter = "\t", pattern = "yyyy-MM-dd hh:mm:ss")
private Date date2;
#DataField(pos = 5, length = 1)
private int numerOfFile;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate1() {
return date1;
}
public void setDate1(Date date1) {
this.date1 = date1;
}
public Date getDate2() {
return date2;
}
public void setDate2(Date date2) {
this.date2 = date2;
}
public int getNumerOfFile() {
return numerOfFile;
}
public void setNumerOfFile(int numerOfFile) {
this.numerOfFile = numerOfFile;
}
}
Order:
import java.math.BigDecimal;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
#FixedLengthRecord(header = OrderHeader.class)
public class Order {
#DataField(pos = 1, delimiter = "\t")
private int lp;
#DataField(pos = 2, delimiter = "\t")
private int id;
#DataField(pos = 3, delimiter = "\t")
private BigDecimal priceByOne;
#DataField(pos = 4, delimiter = "\t")
private BigDecimal count;
#DataField(pos = 5, length = 2)
private int foreignId;
public int getLp() {
return lp;
}
public void setLp(int lp) {
this.lp = lp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BigDecimal getPriceByOne() {
return priceByOne;
}
public void setPriceByOne(BigDecimal priceByOne) {
this.priceByOne = priceByOne;
}
public BigDecimal getCount() {
return count;
}
public void setCount(BigDecimal count) {
this.count = count;
}
public int getForeignId() {
return foreignId;
}
public void setForeignId(int foreignId) {
this.foreignId = foreignId;
}
#Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append("...lp:").append(lp).append("\n");
str.append("...id:").append(id).append("\n");
str.append("...priceByOne:").append(priceByOne).append("\n");
str.append("...count:").append(count).append("\n");
str.append("...foreignId:").append(foreignId).append("\n");
return str.toString();
}
XmlOrder:
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class XmlOrder extends OrderHeader {
public XmlOrder() {
}
private List<Order> line;
public XmlOrder(OrderHeader header, List<Order> lines) {
this.setDate1(header.getDate1());
this.setDate2(header.getDate2());
this.setId(header.getId());
this.setName(header.getName());
this.setNumerOfFile(header.getNumerOfFile());
this.setLines(lines);
}
public List<Order> getLines() {
return line;
}
public void setLines(List<Order> lines) {
this.line = lines;
}
}
Route:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat;
import com.stefan.test.fixedlengthtest.model.Order;
import com.stefan.test.fixedlengthtest.model.XmlOrder;
import com.stefan.test.fixedlengthtest.processors.ProcessOrder;
/**
* A Camel Java8 DSL Router
*/
public class MyRouteBuilder extends RouteBuilder {
public void configure() throws JAXBException {
// Initialize JAXB
JAXBContext jaxbContext = JAXBContext.newInstance(XmlOrder.class);
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(jaxbContext);
//#formatter:off
from("file:testdir").routeId("data-route")
.unmarshal(new BindyFixedLengthDataFormat(Order.class))
.to("log:test?showAll=true")
.log("Loop through data")
.process(new ProcessOrder())
.marshal(jaxbDataFormat)
.log("Order: \n${body}")
.log("DATA processed")
;
}
}
OrderProcessor:
import java.util.List;
import java.util.Map;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.stefan.test.fixedlengthtest.model.OrderHeader;
import com.stefan.test.fixedlengthtest.model.XmlOrder;
public class ProcessOrder implements Processor {
private static final Logger logger = LoggerFactory.getLogger(ProcessOrder.class);
#Override
public void process(Exchange exchange) throws Exception {
Map<String, OrderHeader> headermap = exchange.getIn()
.getHeader(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_HEADER, Map.class);
OrderHeader header = headermap.get(OrderHeader.class.getName());
logger.info("Processing order: {}", header.getId());
logger.info("...date1: {}", header.getDate1());
logger.info("...date2: {}", header.getDate2());
logger.info("...numberOfFile: {}", header.getNumerOfFile());
XmlOrder xmlorder = new XmlOrder(header, exchange.getIn().getBody(List.class));
exchange.getIn().setBody(xmlorder);
}
}
This example route will output something like:
[ main] ProcessOrder INFO Processing order: 31
[ main] ProcessOrder INFO ...date1: 2018-11-01T00:00:00.000+0200
[ main] ProcessOrder INFO ...date2: 2018-10-31T00:05:16.000+0200
[ main] ProcessOrder INFO ...numberOfFile: 3
[ main] data-route INFO Order:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xmlOrder>
<date1>2018-11-01T00:00:00+02:00</date1>
<date2>2018-10-31T00:05:16+02:00</date2>
<id>31</id>
<name>name</name>
<numerOfFile>3</numerOfFile>
<lines>
<count>13</count>
<foreignId>14</foreignId>
<id>10</id>
<lp>1</lp>
<priceByOne>1</priceByOne>
</lines>
<lines>
<count>14</count>
<foreignId>14</foreignId>
<id>20</id>
<lp>2</lp>
<priceByOne>1</priceByOne>
</lines>
<lines>
<count>15</count>
<foreignId>14</foreignId>
<id>30</id>
<lp>3</lp>
<priceByOne>1</priceByOne>
</lines>
</xmlOrder>
[ main] data-route INFO DATA processed
You can check full sources for this in github

I would recommend to use Groovy DSL and MarkupBuilder http://docs.groovy-lang.org/latest/html/api/groovy/xml/MarkupBuilder.html.
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.doubleQuotes = true
xml.mkp.xmlDeclaration(version: '1.0', encoding: 'foo-bar', and-so-on)
xml.orders(){
'id':id,
'name':name,
...
}
}
exchange.in.body = writer.toString()

Related

EXPECTED BEGIN_ARRAY BUT WAS BEGIN_OBJECT AT LINE 1 COLUMN 2 PATH $22

I want to access JSON array . so I created 2 Object !!Have a look at my code , Url
Url-cricapi.com/api/matches/?apikey=JimJAfsmRGOnDpCrRrqO6htlilg1
My MatchesArrayClass
package com.piyushjaiswal.jsonpractis;
public class MatchesArray {
private Matches matches;
private provider provider2;
public MatchesArray(Matches matches, provider provider2) {
this.matches = matches;
this.provider2 = provider2;
}
public Matches getMatches() {
return matches;
}
public void setMatches(Matches matches) {
this.matches = matches;
}
public provider getProvider2() {
return provider2;
}
public void setProvider2(provider provider2) {
this.provider2 = provider2;
}
}
Matches Class
package com.piyushjaiswal.jsonpractis;
import com.google.gson.annotations.SerializedName;
public class Matches {
private int unique_id;
private String date;
private String dateTimeGMT;
#SerializedName("team-1")
private String team1;
#SerializedName("team-2")
private String team2;
private String type;
private String toss_winner_team;
private boolean squad;
private boolean matchStarted;
public int getUnique_id() {
return unique_id;
}
public void setUnique_id(int unique_id) {
this.unique_id = unique_id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDateTimeGMT() {
return dateTimeGMT;
}
public void setDateTimeGMT(String dateTimeGMT) {
this.dateTimeGMT = dateTimeGMT;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getToss_winner_team() {
return toss_winner_team;
}
public void setToss_winner_team(String toss_winner_team) {
this.toss_winner_team = toss_winner_team;
}
public boolean isSquad() {
return squad;
}
public void setSquad(boolean squad) {
this.squad = squad;
}
public boolean isMatchStarted() {
return matchStarted;
}
public void setMatchStarted(boolean matchStarted) {
this.matchStarted = matchStarted;
}
}
My Provider class
package com.piyushjaiswal.jsonpractis;
public class provider {
private String source;
private String url;
private String pubDate;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
MainActivity Class
package com.piyushjaiswal.jsonpractis;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private JsonPlaceHolderApi jsonPlaceHolderApi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textview);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://cricapi.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
getMatchList();
}
private void getMatchList() {
Call<List<MatchesArray>> call = jsonPlaceHolderApi.getPosts("JimJAfsmRGOnDpCrRrqO6htlilg1");
call.enqueue(new Callback<List<MatchesArray>>() {
#Override
public void onResponse(Call<List<MatchesArray>> call, Response<List<MatchesArray>> response) {
if(!response.isSuccessful()){
textView.setText(response.message() + "123");
return;
}
List<MatchesArray> list = response.body();
textView.setText(list.get(0).getMatches().getDate());
}
#Override
public void onFailure(Call<List<MatchesArray>> call, Throwable t) {
textView.setText(t.getMessage() +"22");
}
});
}
}
But output on screenshot is
"Expected BEGIB_ARRAY but was BEGIN_OBJECT at line 1 column 2 patg $2"
Your JSON syntax is wrong. The response starts with {"matches":[, this means it is an object, with the parameter matches that is of type match[].
So, you need a new class along the lines of:
public class MatchesWrapper {
private List<Matches> matches;
}
And change all your Call<List<MatchesArray>> to Call<MatchesWrapper>.
The error you received tells you this. You expected an array of Matches (Expected BEGIN_ARRAY), but instead received an object (was BEGIN_OBJECT).

Hibernate relationship issues

I'm trying to store a couple of objects in a Oracle database using Hibernate. However, I can't seem to get the mapping relations right. As it stands I'm getting a NullPointerException.
In the end, every Client should contain an id, a first and last name, an e-mail and a set of reservations.
Every flight should contain a flight number, a starting time and a starting airport.
The bookings should have an id, client, flight, the date of booking and the amount of booked seats.
At the moment I'm just testing it with a single client as to not over-complicate it but I have gotten rather confused.
Here is what I have done till now:
Main
import java.sql.Date;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAApplication {
private EntityManagerFactory entityManagerFactory;
public JPAApplication() {
Logger.getLogger("org.hibernate").setLevel(Level.ALL);
entityManagerFactory = Persistence.createEntityManagerFactory("DB1");
}
public void testFlights() {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Client firstClient = new Client("Kiro", "Betona", "kirobetona#gmail.com");
em.persist(firstClient);
}
public static void main(String[] args) {
JPAApplication app = new JPAApplication();
app.testFlights();
}
}
Client
import java.util.*;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
#Entity
public class Client {
private int id;
private String firstName;
private String lastName;
private String email;
private Set<Booking> reservations;
Client(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(int id) {
this.id = id;
}
#OneToMany(targetEntity = Booking.class, mappedBy="client", fetch=FetchType.EAGER)
public Set<Booking> getReservations() {
return reservations;
}
public void setReservations(Set<Booking> reservations) {
this.reservations = reservations;
}
}
Booking
import java.util.Date;
import javax.persistence.*;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
#Entity
public class Booking {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private Client client;
private Flight flight;
private int bookedSeats = 1;
#Temporal(TemporalType.DATE)
private Date bookingDate;
Booking(Client client, Flight flight, Date bookingDate) {
this.client = client;
this.flight = flight;
this.bookingDate = bookingDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#ManyToOne
#JoinColumn(name="id")
public Client getClient() {
return client;
}
#OneToOne
#PrimaryKeyJoinColumn(name="flight")
public Flight getFlight() {
return flight;
}
public int getBookedSeats() {
return bookedSeats;
}
public void setBookedSeats(int bookedSeats) {
this.bookedSeats = bookedSeats;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public void setClient(Client client) {
this.client = client;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
}
Flight
import java.sql.Time;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
#Entity
public class Flight {
private String flightNumber;
private Time startingTime;
private String startingAirport;
Flight(String flightNumber, Time startingTime, String startingAirport) {
this.flightNumber = flightNumber;
this.startingTime = startingTime;
this.startingAirport = startingAirport;
}
#Id
#OneToOne(targetEntity = Booking.class, mappedBy="flight", fetch=FetchType.EAGER)
#PrimaryKeyJoinColumn(name="flight")
public String getFlightNumber() {
return flightNumber;
}
public Time getStartingTime() {
return startingTime;
}
public void setStartingTime(Time startingTime) {
this.startingTime = startingTime;
}
public String getStartingAirport() {
return startingAirport;
}
public void setStartingAirport(String startingAirport) {
this.startingAirport = startingAirport;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
}
I see some confusion when you are using the #JoinColumn annotation in Booking: you are telling Booking to use a column named "id" as the Foreign Key to Client, however that conflicts with the Booking primary key.
You could instead make the column name separate, like this:
#JoinColumn(name="clientId")
Also, for Hibernate to instantiate the Entity objects, the entity should have a no-arg constructor. The default no-arg constructors have been overridden in the example code.

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 6 column 11 path $.data

i have json, and i want to retrieve using gson,below my json :
{
"status_code":1,
"message":"response ok",
"api_version":"v1",
"api_endpoint_name":"category_island",
"data": [
{
"island_id":1,
"island_name":"Anambas",
"categories": [
{"id":1, "category_name":"Culinary"},
{"id":2, "category_name":"Hotel"},
{"id":3, "category_name":"Culture"}
]
}
]
}
after that i create model to retrieve it :
public class ModelCategory {
#SerializedName("status_code")
public String status_code;
#SerializedName("message")
public String message;
#SerializedName("api_version")
public String api_version;
#SerializedName("api_endpoint_name")
public String api_endpoint_name;
#SerializedName("data")
public data data;
public static class data {
#SerializedName("id")
public String id;
#SerializedName("island_name")
public String island_name;
#SerializedName("categories")
public List<categories> categories;
public static class categories {
#SerializedName("ID")
public String id;
#SerializedName("category_name")
public String category_name;
}
}
}
and this is my code to retrieve json, i'm using gson:
ModelCategory model = new Gson().fromJson(models, ModelCategory.class);
ArrayList<ModelCategory.data.categories> _model = (ArrayList<ModelCategory.data.categories>) model.data.categories;
but when i run always display error in ModelCategory , error like below :
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 6 column 11 path $.data
You need to change the model. Try this -
Category.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Category {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("category_name")
#Expose
private String categoryName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
#Override
public String toString() {
return "Category [id=" + id + ", categoryName=" + categoryName + "]";
}
}
Datum.java
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum {
#SerializedName("island_id")
#Expose
private Integer islandId;
#SerializedName("island_name")
#Expose
private String islandName;
#SerializedName("categories")
#Expose
private List<Category> categories = new ArrayList<Category>();
public Integer getIslandId() {
return islandId;
}
public void setIslandId(Integer islandId) {
this.islandId = islandId;
}
public String getIslandName() {
return islandName;
}
public void setIslandName(String islandName) {
this.islandName = islandName;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
#Override
public String toString() {
return "Datum [islandId=" + islandId + ", islandName=" + islandName
+ ", categories=" + categories + "]";
}
}
Example.java
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("status_code")
#Expose
private Integer statusCode;
#SerializedName("message")
#Expose
private String message;
#SerializedName("api_version")
#Expose
private String apiVersion;
#SerializedName("api_endpoint_name")
#Expose
private String apiEndpointName;
#SerializedName("data")
#Expose
private List<Datum> data = new ArrayList<Datum>();
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getApiVersion() {
return apiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
public String getApiEndpointName() {
return apiEndpointName;
}
public void setApiEndpointName(String apiEndpointName) {
this.apiEndpointName = apiEndpointName;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
#Override
public String toString() {
return "Example [statusCode=" + statusCode + ", message=" + message
+ ", apiVersion=" + apiVersion + ", apiEndpointName="
+ apiEndpointName + ", data=" + data + "]";
}
}
Now you can test it as -
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.Example;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
/**
* #param args
*/
public static void main(String[] args) {
String s = "{\"status_code\":1,\"message\":\"response ok\",\"api_version\":\"v1\",\"api_endpoint_name\":\"category_island\",\"data\":[{\"island_id\":1,\"island_name\":\"Anambas\",\"categories\":[{\"id\":1,\"category_name\":\"Culinary\"},{\"id\":2,\"category_name\":\"Hotel\"},{\"id\":3,\"category_name\":\"Culture\"}]}]}";
Example info = gson.fromJson(s, Example.class);
System.out.println(info);
}
}
Result is -
Example [statusCode=1, message=response ok, apiVersion=v1, apiEndpointName=category_island, data=[Datum [islandId=1, islandName=Anambas, categories=[Category [id=1, categoryName=Culinary], Category [id=2, categoryName=Hotel], Category [id=3, categoryName=Culture]]]]]

I want to convert csv file to xml

I want to convert csv file to xml and i want to send the converted xml file to a queue in activemq..Is there any sample code or any reference websites or blogs please help to find sample code for this program..
Use the Bindy and Xstream components. From http://workingwithqueues.blogspot.ch/2012/07/converting-csv-to-xml-with-camel-bindy.html (with a JMS endpoint instead of a file endpoint):
DataFormat bindy = new BindyCsvDataFormat("com.package.dto");
from("file://TEST?fileName=Employee.csv")
.unmarshal(bindy)
.marshal()
.xstream()
.to("jms:queue:FOO.BAR");
For connecting to JMS have a look at the JMS and ActiveMQ components.
package org.mycompany.conversion;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Greeting {
#XmlElementWrapper(name = "Person")
#XmlElement(name = "persons")
private List<Person> persons;
public List<Person> getPerson() {
return persons;
}
public void setPerson(List<Person> persons) {
this.persons = persons;
}
}
========================================================================================
package org.mycompany.conversion;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#CsvRecord(separator = ",")
public class Person {
#DataField(pos = 1)
#XmlElement
private int id;
#DataField(pos = 2)
#XmlElement
private String name;
#DataField(pos = 3)
#XmlElement
private int age;
#Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
======================================================================================
package org.mycompany.conversion;
import javax.xml.bind.JAXBContext;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;
public class CsvConversion {
public static void main(String[] args) throws Exception {
JaxbDataFormat xmlDataFormat = new JaxbDataFormat();
JAXBContext con = JAXBContext.newInstance(Greeting.class);
xmlDataFormat.setContext(con);
DataFormat bindy = new BindyCsvDataFormat(Person.class);
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("file:src/main/data/in/csv?noop=true").split().tokenize("\n").unmarshal(bindy)
// constanr(true):-aggregate all using same expression
.aggregate(constant(true), new AttachAggregation())
//mandatory after aggregate
.completionTimeout(100)//end after this gives problem
.marshal(xmlDataFormat).log("xml body is ${body}")
.to("file:src/main/data/in/xml?fileName=convertedXml.xml");// .aggregate(new
// AttachAggreagation());
}
});
context.start();
Thread.sleep(5000);
}
}
=========================================================================================
package org.mycompany.conversion;
import java.util.ArrayList;
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
public class AttachAggregation implements AggregationStrategy {
List<Person> list = new ArrayList();
Greeting gre = new Greeting();
#Override
//person-address
// greeting-user
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
Person newBody1 = newExchange.getIn().getBody(Person.class);
list.add(newBody1);
return newExchange;
} else {
Person newBody2 = newExchange.getIn().getBody(Person.class);
list.add(newBody2);
gre.setPerson(list);
oldExchange.getIn().setBody(gre);
return oldExchange;
}
}
}

org.springframework.data.solr.UncategorizedSolrException: Failed to convert to type org.apache.solr.common.SolrInputDocument

I use spring-data-solr to integrate spring-data-jpa with solr, but when i use SolrOperations to saveBean(org.domain.Article), an exception throws:
org.springframework.data.solr.UncategorizedSolrException: Failed to convert from type org.kb.domain.Article to type org.apache.solr.common.SolrInputDocument for value 'Article [id=1,title=test-1, description=test-1, content=test-1, author=test-1, link=test-1, attachment=test-1, date=Sat Jan 05 20:06:12 CST 2013, category=org.kb.domain.Category#67e6cf07]'; nested exception is org.apache.solr.client.solrj.beans.BindingException: Invalid setter method. Must have one and only one parameter; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type org.kb.domain.Article to type org.apache.solr.common.SolrInputDocument for value 'Article [id=1,title=test-1, description=test-1, content=test-1, author=test-1, link=test-1, attachment=test-1, date=Sat Jan 05 20:06:12 CST 2013, category=org.kb.domain.Category#67e6cf07]'; nested exception is org.apache.solr.client.solrj.beans.BindingException: Invalid setter method. Must have one and only one parameter
here is my bean:
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.solr.client.solrj.beans.Field;
import com.fasterxml.jackson.annotation.JsonFormat;
#Entity
#Table(name="article")
public class Article extends IdEntity{
private static final long serialVersionUID = -5170398606065544445L;
private String title;
private String description;
private String content;
private String author;
private String link;
private String attachment;
private Date date;
private Category category;
public Article() {
super();
}
#ManyToOne
#JoinColumn(name="category_id")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
#Column(name="title")
#Field("title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Column(name="description")
#Field("description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Column(name="content")
#Field("content")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
#Column(name="author")
#Field("author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
#Column(name="link")
#Field("link")
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
#Column(name="attachment")
#Field("attachment")
public String getAttachment() {
return attachment;
}
public void setAttachment(String attachment) {
this.attachment = attachment;
}
#Column(name="date")
#JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+08:00")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
#Override
public String toString() {
return "Article [id=" + id + ",title=" + title + ", description=" + description
+ ", content=" + content + ", author=" + author + ", link="
+ link + ", attachment=" + attachment + ", date=" + date
+ ", category=" + category + "]";
}
}
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.apache.solr.client.solrj.beans.Field;
#MappedSuperclass
public abstract class IdEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5676694680777491651L;
protected Long id;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Field("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
The problem is in your solrj Field annotation. Have a look at the documentation:
The #Field annotation can be applied to a field or a setter method.
You should move the Field annotation to the either the setId setter method or the id field itself. You can even remove the id qualifier since the field name is already id, that's enough:
#Field
protected Long id;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

Resources