How to send multiple objects to webservice using CXF services with JSON - json

i have a webservice which accepts 3 different types of object. i want to pass these objects using JSON........ and acccept the server side as JSON here i convert the objects into java objects.
can any one tell me the code.
My objects are Emploee, Student and cource.....

#POST
#Path("<some path here>")
public Response addBookJaxbJson(
#Multipart(value = "employee", type = "application/json") Employee emp,
#Multipart(value = "student", type = "application/json") Student student,
#Multipart(value = "course", type = "application/json") Course course)
throws Exception {
}
And of course, your client will have to send multipart data, either from a form or constructed somehow.

Related

How to convert Amazon QLDB IonStruct to Json in java?

I have written a QLDB query to fetch a document by document ID So that I want to convert this document to a JSON response and pass it through the rest end point.
qldbDriver.QldbDriver().execute(txn ->{
IonSystem ionSys = IonSystemBuilder.standard().build();
Result result = txn.execute("SELECT * FROM _ql_committed_WALLET WHERE metadata.id = ?",ionSys.newString(id));
IonStruct person = (IonStruct) result.iterator().next();
String s = person.get("data").toPrettyString();
});
Here I want that conversation.
How can I fix this issue?
There are many ways to achieve what you are trying to do. But picking up from your example, you might want to convert your result person into JSON directly, or you might want to use a library to generate that JSON. If it possible to convert from IonValue (of which IonStruct is an instance) to POJOs and then you could convert those to JSON using Jackson.
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
...
IonValue personValue = qldbDriver.QldbDriver().execute(txn ->{
IonSystem ionSys = IonSystemBuilder.standard().build();
Result result = txn.execute("SELECT * FROM _ql_committed_WALLET WHERE metadata.id = ?",ionSys.newString(id));
return (IonStruct) result.iterator().next();
});
Person person = IonObjectMapper.builder().build().readValue(personValue, Person.class);
String personJson = new ObjectMapper().writeValueAsString(person);
In this example we are taking the IonValue as returned from QLDB and converting it to a POJO using the Jackson Ion library. Then we use the regular JSON Jackson library to convert that same Person POJO into a JSON string which you can then send over your REST connection as the body of the response.

Passing Json status code in Springboot+Angular

Is there any way to pass a Json status/response from Springboot to Angular service? Let’s say, beside the Httpstatus sent on Response Entity, I want also to send a Json response. Thanks
Or maybe a second question is how to use the Http status from Spring boot, in Angular service.
From your springboot endpoint, you can send JSON response along with the HttpStatus
Below is the sample code
#GetMapping(value = "/getUserDetails" , produces = "application/json")
public ResponseEntity getUserDetails(){
userDetails = userService.getUserDetails();
return ResponseEntity.status(HttpStatus.OK).body(userDetails);
}
This is how you can return userDetails object which will be converted to JSON automatically as we used produces = "application/json"

Returning non JSON data (List of object) from spring boot controller

I am trying to retrieve OneToMany association in my spring boot project.When I am returning the JSON response from controller it is only getting like normal list of string instead of proper JSON. This result is a join result from a JPQL query,
I am adding the repository method here,
#Query("SELECT ur.userId , r.role FROM Roles r JOIN r.roleJoin ur")
List<Roles> findByRole();
And my controller has the code like the following,
#GetMapping("/check")
public List<Roles> check() {
return repoObj.findByRole();
}
And getting response like this,
[[2,"A"],[649,"B"],[651,"C"],[653,"A"],[658,"A"],[3,"A"],[1,"B"],[670,"B"]]
It is seemd to be a list of object, But defaultly spring boot controller will return the data in JSON format. But I only getting like the following. Since I need to access the JSON from my front end Angular application.
Can anyone help me to clarify that to send response in proper JSON itself instead of just a list ?
The problem is that the projection in your query does not match the return type.
You assume that you get objects of type Roles but in your query you only select ur.nuserId and r.srole_desc
Either change your query to:
#Query("SELECT r FROM Roles r JOIN r.roleUserMappingJoin ur")
but this would be the same as findAll()
or create a DTO that holds the data you want to return.
Read more about projection and Spring Data JPA here:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

Parse Json Response through Dictionary object

I have Json response through Facebook API like this:
Now I want to parse this data so I can use within my game. For this purpose, I have written code up to this:
public void OnChallengesButtonClick ()
{
SoundManager.Instance.PlayButtonClickSound ();
FB.API ("/me/friends", HttpMethod.GET, RetrieveFacebookFriends);
}
public void RetrieveFacebookFriends (IGraphResult result)
{
if (!string.IsNullOrEmpty (result.Error)) {
Debug.Log ("ERROR: " + result.Error);
return;
}
IDictionary<string,object> friendsDict = result.ResultDictionary;
Debug.Log ("raw: " + result.RawResult);
}
So how to extract name and ID data from available Json response through Dictionary object?
Basically you should create a class to serialize/deserialize an object:
class FBResponse
FBDataResponse data;
class FBDataResponse
string name;
string id;
Take a look to my response here for more details (after "Final EDIT" part):
Converting arrays of arrays to json
It should be easy to create those two class and use them to deserialize the FB response. Just remember that your variables will need to match the same name with the json response from Facebook (for example you cannot call data data1).
After you have the object it should be easy to do what you want with it... and with the object you should be able to get the json again.
PS. Unity default json serializer does not support Dictionary that's why you need to serialize two object. If you don't want that you should try to use a different library.
I think this is all I need: JSON with Unity
friends = (List<object>)(((Dictionary<string, object>)friendsH) ["data"]);
if(friends.Count > 0) {
var friendDict = ((Dictionary<string,object>)(friends[0]));
var friend = new Dictionary<string, string>();
friend["id"] = (string)friendDict["id"];
friend["first_name"] = (string)friendDict["first_name"];
}
Before I can't able to find this post from Facebook.

Reading JSON thru web service into POJOs annotated for Hibernate

I am reading the following json through a web service. Is there a way to read the json into three appropriate POJOs? The POJOs are generated by hibernate and are used to communicate to the database.
Basically I need to read the person json into a Person POJO, the pets json into a set of Pet POJOs, and the toy json into a set of Toy POJOs.
The JSON
{
"person":{"first_name":"John", "last_name":"Smith"},
"pets":[{"species":"dog", "name":"Adama"}, {"species":"cat", "name":"Benton"} ],
"toys":[{"car":"corvet", "color":"black"}, {"action_figure":"hancock", "height":"1ft"} ]
}
The Web Service
#Post
public Representation readForm(Representation representation) {
try {
Person aPerson = …
Set<Pet> petSet = …
Set<Toy> toySet = ...
….
You can use xStream . You will have to create a VO having all 3 types of your objects as properties. Give them respective aliases and you will get all 3 types of objects in that VO. You can get them simply by calling their getters.