I have a Film model like below:
public class Film{
private String name;
private String category;
private Actor[] actors;
private MarketingPlan marketingPlan;
}
class Actor{
private String name;
private double salary;
}
class MarketingPlan{
private String nameOfPlan;
private Country[] affectedCountries;
}
class Country{
private int countryID;
private String marketingText;
}
which generate a JSON like this:
{
"name": "My First Film",
"category": "action",
"marketingPlan": {
"name": "plan1",
"affectedCountries": [
{
"marketingText": "This is a marketing text",
"countryID": 332
}
]
},
"actors": [
{
"name": "John",
"salary": 123456
}
]
}
I would like to represent my JSON result above as tree structure in JavaFX. Can anyone give me some hint or suggest me some tutorial how can I do it. Thank you very much in advanced!
Related
I want to return the class at the same level here.
As here id, username, password, etc are under userCredentialEntity and so in userDetailEntity key.
{
"userCredentialsEntity": {
"id": 5,
"username": "testuser3",
"password": "$2a$10$yFmeUcE3uTOf9H4TZqWXfO/b8zsTp6sqnWax5iyRXBhlfXF3dSsk2",
"email": "testuser3#gmail.com",
"roles": [
{
"id": 2,
"name": "ROLE_MANAGER"
}
]
},
"userDetailsEntity": {
"userId": 5,
"first_name": "Test",
"last_name": "Singh 3",
"birth_date": "12",
"birth_month": "01",
"birth_year": "2002",
"area": "Chandani Chowk",
"city": "Sahadra",
"district": "Sonbhadra",
"pin_code": 231325,
"mobile_number": "6788762345"
}
}
I have to return this.
{
"id": 5,
"username": "testuser3",
"email": "testuser3#gmail.com",
"roles": [
{
"id": 2,
"name": "ROLE_MANAGER"
}
],
"first_name": "Test",
"last_name": "Singh 3",
"birth_date": "12",
"birth_month": "01",
"birth_year": "2002",
"area": "Chandani Chowk",
"city": "Sahadra",
"district": "Sonbhadra",
"pin_code": 231325,
"mobile_number": "6788762345"
}
And also neglect the password and userId field while returning
The Code for my implementation is:
#Data
public class UserMerged {
private UserCredentialsEntity userCredentialsEntity;
private UserDetailsEntity userDetailsEntity;
}
UserDetailsEntity userDetailsEntity = userDetailService.fetchUserDetails(userId).get();
UserCredentialsEntity userCredentialsEntity = userCredentialsRepository.findByUsername(username);
UserMerged userMerged = new UserMerged();
userMerged.setUserDetailsEntity(userDetailsEntity);
userMerged.setUserCredentialsEntity(userCredentialsEntity);
return ResponseEntity.ok().body(userMerged);
I am new in Spring Boot and doesn't got any satisfactory answer regarding this. Please suggest me any way of achieving this.
Thank You.
First thing first, Whatever your return type of object is, for that you need to create the specific class and then return it.
In your case Modify your UserMerged class with whatever u would like to return and then set the parameters accordingly.
Likely,
Your UserMerged will be
#Data
public class UserMerged {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer Id;
#Column(name="username")
private String username;
#Column(name="email")
private String email;
#Column(name="roles")
private List<Roles> roles;
#Column(name="first_name")
private String first_name;
#Column(name="last_name")
private String last_name;
#Column(name="birth_date")
private String birth_date;
#Column(name="birth_month")
private String birth_month;
#Column(name="birth_year")
private String birth_year;
#Column(name="area")
private String area;
#Column(name="city")
private String city;
#Column(name="district")
private String district;
#Column(name="pin_code")
private Integer pin_code;
#Column(name="mobile_number")
private Integer mobile_number;
And instead of setting the UserDetailsEntity and UserCredentialsEntity you can set your fields from those two classes to UserMerged class.
userMerged.setId(userCredentialsEntity.getId());
userMerged.setUsername(userCredentialsEntity.getUsername());
userMerged.setEmail(userCredentialsEntity.getEmail());
userMerged.setRoles(userCredentialsEntity.getRoles());
userMerged.setFirst_name(userDetailsEntity.getFirst_name());
userMerged.setLast_name(userDetailsEntity.getLast_name());
userMerged.setBirth_date(userDetailsEntity.getBirth_date());
userMerged.setBirth_month(userDetailsEntity.getBirth_month());
userMerged.setBirth_year(userDetailsEntity.getBirth_year());
userMerged.setArea(userDetailsEntity.getArea());
userMerged.setCity(userDetailsEntity.getCity());
userMerged.setDistrict(userDetailsEntity.getDistrict());
userMerged.setPin_code(userDetailsEntity.getPin_code());
userMerged.setMobile_number(userDetailsEntity.getMobile_number());
This will set all parameters you needed, and you can return the object of user merged object.
Here Their is lot much repetation of code exist so instead so please take care of that or you can also add common parameters into one class.
I have these POJO
class LocaleBook
{
private String bookId;
private String bookName;
private Locale locale;
//setters & getters
}
class Locale
{
private String country;
private String language;
//setters & getters
}
Sample data for the above is
"alice", "Alice in the wonderland", "GB", "en"
"matrix", "The Matrix", "TH", "en"
"christtree", "The Christmas Tree", "GB", "en"
And this method:
List<LocaleBook> localeBooks = repo.getLocalBooks();
I want to group data together by country and language and i created this POJO:
class NewPoJo
{
private String country;
private String language;
private List<KeyValuePair> localeBooks = new HashSet<>();
//setters & getters
}
class KeyValuePair {
private String label;
private String value;
//setters & getters
}
Expected output:
[
{
"country":"GB",
"language":"en",
"localeBooks":[
{
"label":"alice",
"value":"Alice in the wonderland"
},
{
"label":"christtree",
"value":"The Christmas Tree"
}
]
},
{
"country":"TH",
"language":"en",
"localeBooks":[
{
"label":"matrix",
"value":"The Matrix"
}
]
}
]
I tried Collectors.groupingBy by country and language but seems like it will create something like following which is not i want. How can i make it to the expected output i want?
{
"GB_en": [
{
"label": "alice"
"value": "Alice in the wonderland"
},
{
"label": "christtree"
"value": "The Christmas Tree"
}
]
}
You can do it by toMap collector with merge function:
list.stream().collect(Collectors.toMap(LocaleBook::getLocale,
lb -> new NewPoJo(lb.getLocale().getCountry(), lb.getLocale().getLanguage(),
new ArrayList<>(Collections.singletonList(new KeyValuePair(lb.getBookId(), lb.getBookName())))),
(a, b) -> {
a.getLocaleBooks().addAll(b.getLocaleBooks());
return a;
}))
.values();
just you should override equals and hashCode methods in the Locale class.
I am trying to map the following JSON to my POJO using Jackson. I have the following JSON and following POJOs. kindly let me know how to map the JSON to POJO.
JSON string :
{
"Application": {
"id": "0",
"name": "MyApp",
"users": [
{
"User": {
"id": "2",
"name": "Beth Jones"
}
}
],
"groups": [
{
"Group": {
"id": "1",
"name": "SimpleGroup",
"users": [
{
"User": {
"id": "2",
"name": "Beth Jones"
}
}
]
}
}
]
}
}
The POJO according to the client specification is below :
package com.example.custom;
//import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.example.Application;
import com.example.Group;
import com.example.User;
import java.util.Collection;
//#JsonIgnoreProperties(ignoreUnknown = true)
public class MyApplication extends Application {
private Collection<User> users;
private Collection<Group> groups;
public MyApplication(String id, String name) {
super(id, name);
}
public void setUsers(Collection<User> users) {
this.users = users;
}
public void setGroups(Collection<Group> groups) {
this.groups = groups;
}
#Override
public Collection<User> getUsers() {
return this.users;
}
#Override
public User getUser(String userId) {
for (User user: MyParser.myApp.getUsers()) {
if (user.getId().equals(userId))
return user;
}
return null;
}
#Override
public Collection<Group> getGroups() {
return this.groups;
}
#Override
public Group getGroup(String groupId) {
for (Group group: MyParser.myApp.getGroups()) {
if (group.getId().equals(groupId))
return group;
}
return null;
}
#Override
public String toString() {
return "MyApplication{" +
"users=" + users +
", groups=" + groups +
'}';
}
}
Mapping Logic :
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MyParser.myApp = mapper.readValue(rewriter.getText(),MyApplication.class);
The resulting object is not able to capture anything as it is all null. Kindly help. Thanks in advance.
I think you should model your JSON correctly, In the users list you shouldn't specify it again that the key is User, that should be preassumed that a list of users will only contain user, same goes for groups list.
IMHO the JSON should look something like this :
{
"application": {
"id": "0",
"name": "MyApp",
"users": [ . ==> Since this is a user List, it will definitely contains user.
{
"id": "2",
"name": "Beth Jones"
}
],
"groups": [
{
"id": "1",
"name": "SimpleGroup",
"users": [
{
"id": "2",
"name": "Beth Jones"
}
]
}
]
}
}
Now the POJO also needs some modification, I am just adding the bare-minimum POJO.
class Application { <====== Top Level Class
private Long id;
private String name;
private List<User> users; // Application has some Users
private List<Group> groups; // Application has some groups
}
class User {
private Long id;
private String name;
}
class Group {
private Long id;
private String name;
private List<User> users; // Each group has some associated users.
}
Now you can use any standard JSON library for Java and convert your JSON into POJO. This will simplify your structure and you won't face null issues with this structure.
Here's my code
#RequestMapping("/bookList")
public List<Books> list() {
return bookService.booksList();
}
public class Books {
private String author;
private String isbn;
private String title;
}
Current response
[["TOM",123456,"ABC"],["JANE",789000,"CDE"]]
Expected response
"Books": [
{"author": "TOM", "isbn": "123456", "title": "ABC"},
{"author": "JANE", "isbn": "789000", "title": "CDE"}
]
What is the code change I need to make?
Your List<Books> is correctly returned as JSON. Your option would be to wrap it into another object:
public class BookList {
public List<Books> books;
}
and return a BookListinstead of List<Books>.
But to be honest, I would not recommend you do do that. It's not very restful.
session.createQuery returns Object[]. I used Criteria query with projections inside my DAOIMPL class and I got the expected output.
colleagues!
We want to write Rest Client to service which follow the HATEOAS principle. So we have the following HAL+JSON representation and we want to deserialize it using spring-hateoas :
{
"id": "1",
"title": "album title",
"artistId": "1",
"stockLevel": 2,
"_links": {
"self": {"href": "http://localhost:8080/rest/albums/1"},
"artist": {"href": "http://localhost:8080/rest/artist/1"}
},
"_embedded": {
"albums": [{ //can be array or object
"id": "1",
"title": "album title",
"artistId": "1",
"stockLevel": 2,
"_links": {
"self": {"href": "http://localhost:8080/rest/albums/1"}
}
}],
"artist": { //can be array or object
"id": "1",
"name": "artist name",
"_links": {
"self": {"href": "http://localhost:8080/rest/artist/1"}
}
} //....
}
}
We expected the java object like this:
HalResource {
Resource<Album> //entity
List<Link> // _links
List<Resource<BaseEntity>>{ //_embedded
Resource<Album>
Resource<Artist>
....
}
}
So we have custom resource representation with embedded(list of resources) and entity(single resource):
#XmlRootElement(name = "resource")
public class HalResource<EntityType, EmbeddedType> extends Resources<EmbeddedType> {
#JsonUnwrapped
private EntityType entity;
public HalResource() {
}
public HalResource(Iterable<EmbeddedType> content, Link... links) {
super(content, links);
}
public EntityType getEntity() {
return entity;
}
public void setEntity(EntityType entity) {
this.entity = entity;
}
}
DTO classes:
public abstract class BaseEntity{}
#XmlRootElement(name = "album")
public class Album extends BaseEntity {
private String id;
private String title;
private String artistId;
private int stockLevel;
// getters and setters...
}
#XmlRootElement(name = "artist")
public class Artist extends BaseEntity {
private String id;
private String name;
// getters and setters...
}
And we want to get something like this, where Entity will be Artist or Album, but HalResourcesDeserializer return Resource.class with null content.
HalResource<Album, Resource<Entity>> resources =
restClient.getRootTarget().path("albums/1").queryParam("embedded", true).request().accept("application/hal+json")
.get(new GenericType<HalResource<Album, Resource<Entity>>>() {});
By using #JsonTypeInfo and #JsonSubTypes anotations we successfully deserialized our JSON(you can see the example on the github), but we don't want to have some additional type filds and anotattions in our DTO and JSON format.
We see one solution that is create a custom deserializer which can processing that.
So the question is: What is the convenient way to deserialize our JSON(links + embedded container) using spring-hateoas?
We use spring-hateoas 0.16v(but we tried 0.19v) and glassfish jersey 2.22.1
Thank you!