Reputation: 422
I am new to JAX-RS
, I am trying to learn new things about it. I am stuck with one issue regarding, creating a JSON
object in Java Script
, posting it through ajax
to a Java
class using JAX-RS
and annotations
and creating a JSON
file out of it. I am creating a Maven
project for it. Can anyone please suggest me any tutorial for this. I am trying to implement it from past 1 week but unable to do anything.
Any suggestions appreciated.
My POST annotation
in Java
is:
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void post(Message obj){
System.out.println("in post");
String v_id = obj.getID();
String v_email = obj.getEmail();
String v_checkedornot = obj.getCheckedOrNot();
System.out.println("id " + v_id +" email " + v_email + " checkedornot " + v_checkedornot);
}
And my AJAX
POST
is:
var passingObject = {
ID : '123456',
userEmail : 'a.a@a',
ApproverFlag : 'true'
}
var passobj = JSON.stringify(passingObject);
$.ajax({
url: './webapi/messages/post',
type: 'POST',
contentType:"application/json",
data: passobj,
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
},'json');
Upvotes: 1
Views: 2648
Reputation: 55
You have to set the contentType as JSON and in the service side you need to annotate method like below
@Produces(MediaType.APPLICATION_JSON) // produces JSON object as response
@Consumes(MediaType.APPLICATION_JSON)
Upvotes: -1
Reputation: 208964
Just map the Javascript objects to Java objects. Here is basic mapping. It's pretty simple.
Javascript Object maps to Java object (or POJO)
var obj = {};
public class MyObject {}
Javascript properties map to Java fields/properties
var obj = {
firstName: "Tejas",
lastName: "Saitwal"
}
public class MyObject {
private String firstName;
private String lastName;
// getters-setters
}
Javascript arrays map to Java List
or Java array.
var obj = {
firstName: "Tejas",
lastName: "Saitwal",
hobbies: ["dancing", "singing"]
}
public class MyObject {
private String firstName;
private String lastName;
private List<String> hobbies;
// getters-setters
}
Javascript nested objects map to Java nested objects
var obj = {
firstName: "Tejas",
lastName: "Saitwal",
hobbies: ["dancing", "singing"],
address: {
street: "1234 main st",
city: "NYC"
}
}
public class MyObject {
private String firstName;
private String lastName;
private List<String> hobbies;
private Address address;
// getters-setters
}
public class Address {
private String street;
private String city;
}
Javascript lists of objects map to Java List<Type>
or Type[]
var obj = {
firstName: "Tejas",
lastName: "Saitwal",
hobbies: ["dancing", "singing"],
address: {
street: "1234 main st",
city: "NYC"
},
friends: [
{ name: "friend1", phone: "123456578" },
{ name: "friend2", phone: "123454567" }
]
}
public class MyObject {
private String firstName;
private String lastName;
private List<String> hobbies;
private Address address;
private List<Friend> friends;
// getters-setters
}
public class Address {
private String street;
private String city;
}
public class Friend {
private String name;
private String phone;
}
Now you have a Java class (MyObject
) that maps cleanly with the Javascript object. So you can have MyObject
as a method parameter.
@POST
@Consumes("application/json")
public Response post(MyObject obj) {}
$.ajax({
url: url,
contentType: "application/json",
data: JSON.stringify(obj)
});
That's not all. You need a provider (or MessageBodyReader
) that knows how to deserialize the JSON into your POJO. For that Jackson is my preferred way to go. Just add this Maven dependency
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.6.0</version>
</dependency>
Then you need to register the JacksonJsonProvider
with your application. There are a bunch of ways this can be done, but without know the JAX-RS implementation and version you are using, I would have to list all the different ways. So if you are unsure about how to register it, please let me know the JAX-RS implementation you are using, the version of the implementation, and show how you are currently configuring your application (i.e. web.xml or Java config).
See Also:
Upvotes: 2