I'm using Gson to manage my Json file. I have a User class which looks like this:
public class User {
private StringProperty username;
private StringProperty password;
...
public User() {}
public User(String user, String pass, ...) {
this.username = new SimpleStringProperty(user);
this.password = new SimpleStringProperty(pass);
...
}
public String getUsername() {
return username.get();
}
public void setUsername(String username) {
this.username.set(username);;
}
...
}
And this is how I add a User to the Json file
public static boolean addUser(User user) throws IOException{
String users = new String(Files.readAllBytes(Paths.get("users.json")));
List<User> userList = getUsers(users);
if (userList.contains(user)) return false;
userList.add(user);
Writer writer = new FileWriter("users.json");
Gson gson = new GsonBuilder().create();
gson.toJson(userList, writer);
writer.close();
return true;
}
gson.toJson(userList, writer) is throwing this error:
Exception in thread "JavaFX Application Thread" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object javafx.beans.property.SimpleStringProperty.bean accessible: module javafx.base does not "opens javafx.beans.property" to unnamed module #4bf59938
I know it has something to do with the StringProperty attributes, but I don't know what's wrong.
Related
I'm using Spring Boot 2.1 with Java 11. I have annotated my User model with fasterxml annotations so that my password can be accepted for POST requests, but not returned for other REST requests ...
#Data
#Entity
#Table(name = "Users")
public class User implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String firstName;
private String lastName;
#NotBlank(message = "Email is mandatory")
#Column(unique=true)
private String email;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
private boolean enabled;
private boolean tokenExpired;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(
name = "users_roles",
joinColumns = #JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(
name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getUsername() {
return email;
}
#Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return false;
}
#PrePersist #PreUpdate
private void prepare(){
this.email = this.email.toLowerCase();
}
}
However, when trying to run an integration test, the password is not getting translated by "objectMapper.writeValueAsString". Here is my test ...
#SpringBootTest(classes = CardmaniaApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
public class UserControllerIntegrationTest {
#Autowired
private TestRestTemplate restTemplate;
#Autowired
private ObjectMapper objectMapper;
#Autowired
private IUserRepository userRepository;
#Test
#WithUserDetails("me#example.com")
void registrationWorksThroughAllLayers() throws Exception {
final String email = "newuser#test.com";
final String firstName = "first";
final String lastName = "last";
final String password = "password";
User user = getTestUser(email, password, firstName, lastName, Name.USER);
ResponseEntity<String> responseEntity = this.restTemplate
.postForEntity("http://localhost:" + port + "/api/users", user, String.class);
assertEquals(201, responseEntity.getStatusCodeValue());
final User createdUser = userRepository.findByEmail(email);
assertNotNull(createdUser);
assertNotNull(createdUser.getPassword());
}
#Test
#WithUserDetails("me#example.com")
void getDetailsAboutMyself() throws JsonProcessingException, JSONException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails user = (UserDetails) authentication.getPrincipal();
final User foundUser = userRepository.findByEmail(user.getUsername());
ResponseEntity<String> responseEntity = this.restTemplate
.getForEntity("http://localhost:" + port + "/api/users/" + foundUser.getId(), String.class);
assertEquals(200, responseEntity.getStatusCodeValue());
// assert proper response
final String userAsJson = objectMapper.writeValueAsString(user);
assertEquals(userAsJson, responseEntity.getBody());
JSONObject object = (JSONObject) new JSONTokener(userAsJson).nextValue();
// Verify no password is returned.
assertNull(object.getString("password"));
}
...
}
The JSON from the objectMapper.writeValueAsString call is
{"id":null,"firstName":"first","lastName":"last","email":"newuser#test.com","enabled":true,"tokenExpired":false,"roles":null,"username":"newuser#test.com","authorities":null,"accountNonExpired":false,"accountNonLocked":false,"credentialsNonExpired":false}
What's the proper way to get my password included as part of the mapping as well as suppressing the password when requesting my entity from read endpoints?
This is a common misunderstanding, there was even a bug report for it and they clarified the documentation.
"READ" and "WRITE" are to be understood from the perspective of the Java Property, i.e., when you serialize an Object, you have to read the property and when you deserialize it, you have to write it.
In your case, you want #JsonProperty(access = JsonProperty.Access.READ_ONLY)
Using WRITE_ONLY or READ_ONLY will not work in your case. The reason is one call over http needs both. Lets take this.restTemplate.postForEntity as an example. In the sending side, your User java object need to serialised to json so it needs the READ and when the rest endpoint receives the json, it need to deserialise the json into User java object so needs the WRITE. It will be the same scenario for this.restTemplate.getForEntity too
One solution is to set the password field of User on the GET endpoint to null before returning
Another solution is create a separate UserDto without password field and return it from GET endpoint
Another solution is to create two JsonViews where one is with password and other one is without password. Then annotate your endpoints with correct #JsonView
How can I save Raw Json as String in the MsSql db with the POST request - using Jackson ObjectMapper to convert the string to Json but not able to change raw json into string?
{
"id": 1,
"someName":"someName",
"json": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossTerm": "Standard Generalized Markup Language"
}
},
"anotherjson":{
"name":"someone",
"age": 121
},
"somedate":"03-11-2019.00:00:00"
}
How can I save this save json as integer, varchar, string, string, date column in the db?
1,someName, "{"title": "example glossary","GlossDiv": {"title": "S","GlossTerm": "Standard Generalized Markup Language"}","{"name":"someone","age": 121}", 03-11-2019.00:00:00.
** Update **
For simplicity here is the simple json
{
"id":1,
"jsonObjectHolder":{
"name": "Name",
"age" : 404
}}
Controller:
#PostMapping("/postJson")
public void postJson(#RequestBody TryJson tryJson) {
tryJsonService.postJson(tryJson);
}
Service:
public void postJson(TryJson tryJson) {
tryJsonRepository.save(tryJson);
}
Repo:
public interface TryJsonRepository extends CrudRepository<TryJson, Integer> {
}
Model:
#Entity
#Table(name = "TryJson")
public class TryJson {
#Id
#Column(name = "id")
private Integer id;
#JsonIgnore
#Column(name = "json_column")
private String jsonColumn;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, String jsonColumn) {
this.id = id;
this.jsonColumn = jsonColumn;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
#Override
public String toString() {
return String.format("TryJson [Id=%s, JsonColumn=%s, jsonObjectHolder=%s]", id, jsonColumn, jsonObjectHolder);
}
}
http://localhost:8080/api/postJson
ID JSON_COLUMN
1 null
Not sure what I am missing here. I do get jsonObjectHolder populated during the debugging but then still I get NULL
TryJson [Id=1, JsonColumn=null, jsonObjectHolder={"name":"Name","age":404}]
Update 2
I am getting null pointer exception.
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Exception occurred inside getter of com.example.tryjson.tryjson.model.TryJson.jsonColumn; nested exception is org.hibernate.PropertyAccessException: Exception occurred inside getter of com.example.tryjson.tryjson.model.TryJson.jsonColumn] with root cause
java.lang.NullPointerException: null
at com.example.tryjson.tryjson.model.TryJson.getJsonColumn(TryJson.java:52) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_192]
Here is my new model
#Entity
#Table(name = "TryJson")
public class TryJson {
private Integer id;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, JsonNode jsonObjectHolder) {
this.id = id;
this.jsonObjectHolder = jsonObjectHolder;
}
#Id
#Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Transient
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
#Column(name = "json_column")
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
}
You could define a JsonNode json property to hold the part you want to persist as text, then mark it as #Transient so JPA does not try to store it on database. However, jackson should be able to translate it back and forward to Json.
Then you can code getter/setter for JPA, so you translate from JsonNode to String back and forward. You define a getter getJsonString that translate JsonNode json to String. That one can be mapped to a table column, like 'json_string', then you define a setter where you receive the String from JPA and parse it to JsonNode that will be avaialable for jackson.
Do not forget to add #JsonIgnore to getJsonString so Jackson does not try to translate to json as jsonString.
#Entity
#Table(name = "request")
public class Request {
private Long id;
private String someName;
#Transient
private JsonNode json;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
#Column(name ="someName")
public String getSomeName() {
return name;
}
public void setSomeName(String name) {
this.name = name;
}
public void setId(Long id) {
this.id = id;
}
// Getter and setter for name
#Transient // This is for Jackson
public JsonNode getJson() {
return json;
}
public void setJson(JsonNode json) {
this.json = json;
}
#Column(name ="jsonString")
public String getJsonString() { // This is for JPA
return this.json.toString();
}
public void setJsonString(String jsonString) { // This is for JPA
// parse from String to JsonNode object
ObjectMapper mapper = new ObjectMapper();
try {
this.json = mapper.readTree(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
UPDATE:
If you mark jsonColumn with #Column spring will use reflection to pull out the data with default initialization null, getJsonColumn translation will never be executed:
#JsonIgnore
#Column(name = "json_column")
private String jsonColumn;
You do not need a jsonColumn, just make sure you mark your setters with #Column, so spring uses gettets/setters to persist to database, when persisting, jpa will execute getJsonColumn, when reading, jpa will execute setJsonColumn and jsonNode will be translated back and forward to string:
#Entity
#Table(name = "TryJson") public class TryJson {
private Integer id;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, String jsonColumn) {
this.id = id;
this.jsonObjectHolder = // use mapper to create the jsonObject;
}
#Id
#Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
#Column(name = "json_column")
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
#Override
public String toString() {
return String.format("TryJson [Id=%s, JsonColumn=%s, jsonObjectHolder=%s]", id, jsonColumn, jsonObjectHolder);
}
}
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<User>> typeReference = new TypeReference<List<Your_Entity>>() {};
InputStream inputStream = TypeReference.class.getResourceAsStream("/bootstrap.json");
try {
List<Your_Entity> users = mapper.readValue(inputStream, typeReference);
log.info("Saving users...");
userService.saveAllUsers(users);
log.info(users.size() + " Users Saved...");
} catch (IOException e) {
log.error("Unable to save users: " + e.getMessage());
}
I use jackson-databind 2.8.0
I have object with Generic Data
public class JsonItem<T> implements Serializable {
private static final long serialVersionUID = -8435937749132073097L;
#JsonProperty(required = true)
private boolean success;
#JsonProperty(required = false)
private T data;
#JsonProperty(required = false)
private Map<String, String> errors = new HashMap<>();
JsonItem() {
}
public boolean getSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Map<String, String> getErrors() {
return errors;
}
public void setErrors(Map<String, String> errors) {
this.errors = errors;
}
}
and have Object
#JsonInclude(JsonInclude.Include.NON_EMPTY)
public class DepositInfoDto implements Serializable {
private static final long serialVersionUID = -4123441934244992311L;
#NotNull
#JsonProperty(required = true)
private String productName;
#NotNull
#JsonProperty(required = true)
private String contractName;
#NotNull
#JsonProperty(required = true)
private List<ContractDto> contracts;
#NotNull
#JsonProperty(required = true)
private StatusDto status;
//...getters and setters
}
I recevied object like JsonItem<List<DepositInfoDto>>.
I try to create universal method to deserealize
public <T> List<T> getObjects(){
ObjectMapper mapper = new ObjectMapper();
List<T> myObjects = mapper.readValue(jsonInput, new TypeReference<JsonItem<List<T>>(){});
return myObjects;
}
Not work because T cast to Object in runtime
public List<DepositInfoDto> getObjects(){
ObjectMapper mapper = new ObjectMapper();
List<DepositInfoDto> myObjects = mapper.readValue(jsonInput, new TypeReference<JsonItem<List<DepositInfoDto >>(){});
return myObjects;
}
work but i want universal method because i have DepositInfoDto, CardinfoDto, ContractDto etc.
I see method
public List<T> getObjects(Class<T> clazz){
ObjectMapper mapper = new ObjectMapper();
List<T> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
return myObjects;
}
but didn't work because i have JsonItem with data List<T>
How can i resolve this problem? Maybe mapper.getTypeFactory() have complex method like mapper.getTypeFactory().constructType(JsonItem.class, List.class,DepositInfoDto.class)
EDIT
In my case
ObjectMapper mapper = new ObjectMapper();
try {
JsonItem<T> item = mapper.readValue(objectWrapper.get(0), mapper.getTypeFactory().constructParametricType(
JsonItem.class, mapper.getTypeFactory().constructCollectionType(List.class, resourceClass)));
return item.getData();
} catch (IOException e) {
LOG.error("Can't deserialize JSON to class: "+ resourceClass +". Error: " + e);
Thread.currentThread().interrupt();
}
You can use TypeFactory#constructParametricType to create a JavaType for JsonItem<T> and then use TypeFactory#constructCollectionType to create CollectionType for List<JsonItem<T>>. Following is the example:
public <T> List<JsonItem<T>> getObjects(String jsonInput, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(
List.class, mapper.getTypeFactory().constructParametricType(JsonItem.class, clazz)));
}
I am trying to run spark job locally to read mysql table contents (in local machine) to jdbcRDD. From online i gathered the below source code and customised to read element table and load all the columns.
private static final JavaSparkContext sc = new JavaSparkContext(
new SparkConf().setAppName("SparkJdbc").setMaster("local[*]"));
private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private static final String MYSQL_CONNECTION_URL = "jdbc:mysql://localhost:3306/ddm";
private static final String MYSQL_USERNAME = "root";
private static final String MYSQL_PWD = "root";
public static void main(String[] args) {
DbConnection dbConnection = new DbConnection(MYSQL_DRIVER,
MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
// Load data from MySQL
JdbcRDD<Object[]> jdbcRDD = new JdbcRDD<>(sc.sc(), dbConnection,
"select * from element where elementid >= ? and elementid <= ?",
1000, 1100, 10, new MapResult(),
ClassManifestFactory$.MODULE$.fromClass(Object[].class));
// Convert to JavaRDD
JavaRDD<Object[]> javaRDD = JavaRDD.fromRDD(jdbcRDD,
ClassManifestFactory$.MODULE$.fromClass(Object[].class));
// Join first name and last name
List<String> employeeFullNameList = javaRDD.map(
new Function<Object[], String>() {
private static final long serialVersionUID = 1L;
#Override
public String call(final Object[] record) throws Exception {
return record[2] + " " + record[3];
}
}).collect();
for (String fullName : employeeFullNameList) {
System.out.println(fullName);
}
}
static class DbConnection extends AbstractFunction0<Connection> implements
Serializable {
private static final long serialVersionUID = 1L;
private String driverClassName;
private String connectionUrl;
private String userName;
private String password;
public DbConnection(String driverClassName, String connectionUrl,
String userName, String password) {
this.driverClassName = driverClassName;
this.connectionUrl = connectionUrl;
this.userName = userName;
this.password = password;
}
#Override
public Connection apply() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
}
Properties properties = new Properties();
properties.setProperty("user", userName);
properties.setProperty("password", password);
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionUrl,
properties);
} catch (SQLException e) {
}
return connection;
}
}
static class MapResult extends AbstractFunction1<ResultSet, Object[]>
implements Serializable {
private static final long serialVersionUID = 1L;
public Object[] apply(ResultSet row) {
return JdbcRDD.resultSetToObjectArray(row);
}
}
However, when I execute the code i get below NullPointerException.
15/08/01 08:27:23 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)java.lang.NullPointerException
at org.apache.spark.rdd.JdbcRDD$$anon$1.<init>(JdbcRDD.scala:79)
at org.apache.spark.rdd.JdbcRDD.compute(JdbcRDD.scala:74)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:35)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:63)
at org.apache.spark.scheduler.Task.run(Task.scala:70)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
I have looked up the jdbcrdd.scala code in github and at line 79 it points to SQL stmt.
val stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
So, the above statement is failing. I have given the required details, but it is throwing null exception. Can anyone help me where i am doing wrong?
I have overlooked my import statements. After i have added below code I am able to execute the spark program locally.
import java.sql.{PreparedStatement, Connection, ResultSet}
I am using the below code to receive Tweets from Twitter4j Search API in the form of JSON response. I am receiving the result in the form of List as specified in Twitter4j search API in the line
List<Status> tweets = result.getTweets();
The problem is that the tweets returned as List where one Status entry is having non-empty and non-null GeoLocation whereas another Status entry is having a null or empty GeoLocation. Since to retrieve the relevant fields from each Status entry (i.e. Tweet), I iterate over the List and call getters which is throwing me null for the Status entries where the GeoLocation field is null.
The approach I tried to follow:
I created a POJO TweetJSON_2 (defined at the bottom of the post) with the relevant fields and their getters and setters. I am using Jackson ObjectMapper to handle null values like below:
JsonGenerator generator = new JsonFactory().createGenerator(os);
generator.setPrettyPrinter(new DefaultPrettyPrinter());
TweetJSON_2 rawJSON;
ObjectMapper mapper = new ObjectMapper();
//mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.setSerializationInclusion(Include.NON_NULL);
// ... rawJSON is populated ...
mapper.writeValue(generator, rawJSON);
However, when I am trying to get the geoLocation field from Status, using the below line which is marked with **
List<Status> tweets = result.getTweets();
I am getting the Java NullPointerException as follows:
[Mon Apr 20 11:32:47 IST 2015]{"statuses":[{"retweeted_status":{"contributors":null,"text":"<my text>",**"geo":null**,"retweeted":false,"in_reply_to_screen_name":null,"truncated":false,"lang":"en","entities":{"symbols":[],"urls":[],"hashtags": ... &include_entities=1","since_id_str":"0","completed_in":0.029}}
**Exception in thread "main" java.lang.NullPointerException
at analytics.search.twitter.SearchFieldsTweetsJSON_2.main(SearchFieldsTweetsJSON_2.java:78)**
For example: If I input a json String as
String s = "{\"first\": 123, \"second\": [{\"second_first\":null, \"second_second\":null}, {\"second_third\":null}, null], \"third\": 789, \"fourth\":null}";
The output should be like
"{\"first\": 123, \"third\": 789}";
What I want, is to replace all null elements from JSONArrays and all null key-value pairs from JSONObjects no matter at whatever level they are nested in my JSON response.
Object vs Tree Model Approach
I tried the Object Model parsing mechanism which is a javax.json.stream.JsonParser.Event based method but would need multiple times of access and object replacement on the JSON String depending on at what level the null is nested making this approach very complicated. At the same time if I use Tree Model mechanism, the entire JSON response would have to be stored as a Tree which may overflow my JVM heap memory because the JSON size can be pretty large based on my query parameters. I need to find a workable solution to overcome this problem. Any suggestions on solving the above discussed problem will be highly appreciated.
The code is as follows:
public class SearchFieldsTweetsJSON_2 {
/* Searches specific fields from Tweets in JSON format */
public static void main(String[] args) throws FileNotFoundException, IOException {
if (args.length < 2) {
System.out.println("java twitter4j.examples.search.SearchTweets [query][outputJSONFile]");
System.exit(-1);
}
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXXXXXXXXX")
.setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.setOAuthAccessToken("NNNNNNNNN-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.setJSONStoreEnabled(true);
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
try {
Query query = new Query(args[0]);
QueryResult result;
File jsonFile = new File(args[1]);
System.out.println("File Path : " + jsonFile.getAbsolutePath());
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(jsonFile));
JsonGenerator generator = new JsonFactory().createGenerator(os);
generator.setPrettyPrinter(new DefaultPrettyPrinter());
TweetJSON_2 rawJSON;
ObjectMapper mapper = new ObjectMapper();
//mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.setSerializationInclusion(Include.NON_NULL);
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
rawJSON = new TweetJSON_2();
rawJSON.setStatusId(Long.toString(tweet.getId()));
rawJSON.setUserId(Long.toString(tweet.getUser().getId()));
rawJSON.setUserName(tweet.getUser().getScreenName());
rawJSON.setStatusText(tweet.getText());
rawJSON.setGeoLocation(tweet.getGeoLocation().toString()); **<< Giving error at tweet.getGeoLocation() since GeoLocation is null**
mapper.writeValue(generator, rawJSON);
System.out.println(rawJSON.toString());
}
} while ((query = result.nextQuery()) != null);
generator.close();
System.out.println(os.toString());
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets : " + te.getMessage());
System.exit(-1);
}
}
}
I have defined my TweetJSON_2 Java object as follows:
public class TweetJSON_2 {
public String statusId;
public String statusText;
public String userId;
public String userName;
public String geoLocation;
public String getStatusId() {
return statusId;
}
public void setStatusId(String statusId) {
this.statusId = statusId;
}
public String getStatusText() {
return statusText;
}
public void setStatusText(String statusText) {
this.statusText = statusText;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getGeoLocation() {
return geoLocation;
}
public void setGeoLocation(String geoLocation) {
this.geoLocation = geoLocation;
}
#Override
public String toString() {
return "TweetJSON_2 [ statusId = " + statusId + ", statusText = " + statusText + "]";
}
}
I have tried with reconfiguring my POJO in the below way and it successfully replaced all the nulls as specified in the setter methods. Didn't need to follow either Tree or Event-based model parsing of JSON string. HTH
The modified TweetJSON_2 POJO:
public class TweetJSON_2 {
public Long statusId = null;
public String statusText = null;
public Long userId = null;
public String userName = null;
public GeoLocation geoLocation = null;
public Long getStatusId() {
if (this.statusId==null)
return new Long(0L);
return statusId;
}
public void setStatusId(Long statusId) {
if (statusId==null)
this.statusId = new Long(0L);
else
this.statusId = statusId;
}
public String getStatusText() {
if (this.statusText==null)
return new String("");
return statusText;
}
public void setStatusText(String statusText) {
if (statusText==null)
this.statusText = new String("");
else
this.statusText = statusText;
}
public Long getUserId() {
if (this.userId==null)
return new Long(0L);
return userId;
}
public void setUserId(Long userId) {
if (userId==null)
this.userId = new Long(0L);
else
this.userId = userId;
}
public String getUserName() {
if (this.userName==null)
return new String("");
return userName;
}
public void setUserName(String userName) {
if (userName==null)
this.userName = new String("");
else
this.userName = userName;
}
public GeoLocation getGeoLocation() {
if (this.geoLocation==null)
return new GeoLocation(0.0,0.0);
return geoLocation;
}
public void setGeoLocation(GeoLocation geoLocation) {
if (geoLocation==null)
this.geoLocation = new GeoLocation(0.0,0.0);
else
this.geoLocation = geoLocation;
}
#Override
public String toString() {
return "TweetJSON_2 [ statusId = " + statusId + ", statusText = " + statusText + "]";
}
}