Provide a GET method in JSON and XML - java

I'm developing REST services using Jersey.
So, if I have an object of User type (which contains information about a User), like:
User userObj = new User();
And I want to provide that information by a GET method, in both JSON and XML.
I already can provide it in JSON, by using gson.toJson(userObj). And what about XML?
Thanks

Take a look at the JAXB API. It provides a way to map XML to classes with simple getter/setter methods. http://jaxb.java.net/tutorial/section_1_1-Introduction.html#About%20JAXB

Related

REST response with selective json properties from json

I have an entity like
public class Pojo{
int id;
org.json.JSONObject json;
/* other properties */
}
Now what I want to do is to send this Pojo object back as JSON BUT in a selective way so that an admin user has access to all properties and a regular user has selective access.
I can do that by using Spring JSONView and creating different views for admin and a regular user. However, the problem which i am stuck at that how can i add selective properties for admin/regular user from JSONObject? The only possible way i could think of is to create a another Pojo to map that object and then use that object in response. But I want to make sure that there is no other way to approach this.
P.S: its a spring boot application
I'm not sure if it works with JsonObject but you can give this a try. Would be interesting to know if it works with "dynamic" objects.
https://github.com/Antibrumm/jackson-antpathfilter
(I'm the creator of that project)

Is there a dynamic data structure that can be automatically serialized to XML or JSON?

I'm developing a restful web-service that should be able to return JSON or XML responses upon request. Of course, the JSON response should be identical to the XML response when the data is compared.
The thing is that I can't use a Java pojo because the returned data fields are dynamic, they are unpredictable.
For example, a specific user may have the following response:
{
"propertyA": "propertyA-Value",
"propertyB": "propertyB-Value",
}
...another user may have:
{
"propertyA": "propertyA-Value",
"propertyB": "propertyB-Value",
"propertyC": "propertyC-Value",
}
...or the XML representation would be
<results>
<propertyA>propertyA-Value</propertyA>
<propertyB>propertyB-Value</propertyB>
<propertyC>propertyC-Value</propertyC>
</results>
Is there a way to automatically serialize the structure holding the previously mentioned data, to JSON or XML. By "automatically", I mean using an API that would work with whatever fields provided.
I can't use an array\list of feature-name\feature-value structures as the service consumer needs to receive the response as mentioned.
use codehaus fasterxml object mapper. A sample app can be seen from below link
https://github.com/abhishek24509/JsonMapper
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
the above code will help to hold dyamic response. Your pojo can have all possible field. But during mapping it will ignore unknown

retrieving custom object from jBPM6 using REST API

we have created a jBPM workflow where we are passing custom object to create a workitem, we are passing this custom object as Map params.
Now using REST API "List getTasksAssignedAsPotentialOwnerByStatus" we can retrieve the TaskSummary for assigned userId, here TaskSummary object is predefined with fields, can anybody please guide me if i want to customize my response (i.e. if i want to retrieve additional parameters in the TaskSummary) then how can i do it using REST API?
The task summary should link a content id (in task data), containing the parameters you passed. Use this content id to get the content.

How to add JSP form data into a JSON object and store it in a database?

I have a user entry form with the following fields :
Name
Age
Address
I want to convert its value to JSON and save it in a database. How can I achieve this?
Which library can I use ?
At server side in servlet save all request parameters to corresponding bean and then you can convert using google's gson library for more info check this tutorial
For example:
You need servlet that takes form submission
To work with JSON I recommend eclipse source minimal JSON, I like it
database - plain JDBC, hibernate or JPA ..
Refer the following steps:
In JSP page, at javascript convert the form fields to JSON using JSON.stringify() method.
At server parse the JSON using appropriate JSON parsers like GSON or Jackson.
After parsing you will get desired object.
Perform database operation on the object.
You will probably need GSON jar , Jackson jar.

Limiting Fields in JSON Response for REST API?

I am using Spring and Java and implementing REST Based services. I have a set of developers who develop for mobile,iPad and Web too. Consider I have a bean
Class User{
private String Name;
private Integer id;
private String photoURL;
private ArrayList<String> ProjectName;
private ArrayList<String> TechnologyList;
private ArrayList<String> InterestList;
//Getters and setters
}
While the Web Developers need the entire fields and mobile developers just require two fields from it whereas the iPad requires something in between mobile and web.
Since I am using jackson as a parser, is there a way where while requesting to the controller I can specify which all data I require and avoid the others. For example consider I do a GET request like
GET>http://somedomain.com/users?filter=name,id,photoUrl
Which returns me a JSON structure something like
{
"name":"My Name",
"id":32434,
"photoUrl":"/sss/photo.jpg"
}
Sameway if someone asks for some more fields, they could be filtered. Please let me know how this can be done so that my API remains generic and useable for all.
You can achieve what you want but some extra work is necessary. I can offer you two solutions.
1. Return a Map
Simply put every property that is requested into the map.
2. Use Jacksons Object Mapper directly
Jackson lets you set filters that specify which properties are serialized or ignored.
FilterProvider filter = new SimpleFilterProvider().addFilter("myFilter",
SimpleBeanPropertyFilter.filterOutAllExcept(requestedProperties));
String json = objectMapper.writer(filter).writeValueAsString(value);
You can then return the JSON string directly instead of an object.
For both solutions you would ideally write a class that does the job. But if you do that you could as well write your own message converter. You could extend the MappingJackson2HttpMessageConverter, for instance, and overwrite the writeInternal method to suit your needs. That has the big advantage that you don't need to change your controllers.
The straightforward solution is to implement custom Jackson JSON serializer that will get field names that should be serialized from thread local storage and then serialize only fields which names are presented in that context. For other hand, in controller you can grab all allowed fields names from url and store them into thread local context. Hope this helps.

Categories

Resources