I am pretty new to REST programming.
Below is my class which is intended to return XML/JSON but I am having some difficulties to make it return proper values. I tried returning Response, JsonArray
and Object of my POJO
class but its not working. I looked several threads but not able to figure out what exactly is the problem.
The resource class:
public class UserService {
UserDBHandler userDBHandler;
Friend f;
@GET
@Path("users/{userId}/friends")
// @Produces(MediaType.TEXT_PLAIN)
@Produces({ MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Friend getFriends(@PathParam("userId") String userId) throws SQLException, ClassNotFoundException
{
System.out.println("userId : " +userId);
userDBHandler = new UserDBHandler();
f = new Friend();
ArrayList<String> userList = userDBHandler.fetchUsers(userId);
System.out.println("Array size: "+userList.size());
Iterator<String> iterator = userList.iterator();
while(iterator.hasNext())
{
f.setUri(iterator.next());
System.out.println(f.getUri());
}
//JsonObject object = Json.createObjectBuilder().add("frienduri",f.getUri()).build();
//ResponseBuilder response = Response.ok(f);
//return Json.createArrayBuilder().add(object).build()
//return response.build();
return f;
}
}
The POJO class:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement
public class Friend{
private String friendURI;
private String event;
private String uri;
String getUri() {
return uri;
}
void setUri(String uri) {
this.uri = uri;
}
String getFriendURI() {
return friendURI;
}
void setFriendURI(String friendURI) {
this.friendURI = friendURI;
}
String getEvent() {
return event;
}
void setEvent(String event) {
this.event = event;
}
}
This is what I get when I return the Response or the Friend object:
**<?xml version="1.0" encoding="UTF-8" standalone="yes"?><friend/>**
And when returning JsonArray
this is what I get:
[{"frienduri":{"string":"b@b.com","chars":"b@b.com","valueType":"STRING"}}]
Another problem I am facing is: if I create a constructor I get below error:
A MultiException has 1 exceptions. They are:
1. java.lang.NoSuchMethodException
: Could not find a suitable constructor in repository.resources.UserService class
.
My dev environment is Tomcat 8 , JDK 1.8, Eclipse Luna.
I am not using Maven or web.xml instead I have an Application class.
I am using jaxrs-ri-2.13 and jackson jars:
- jackson-core-asl-1.9.2
- jackson-jaxrs-1.9.2
- jackson-mapper-asl-1.9.2
- jackson-xc-1.9.2
Json jars
- javax.json-api-1.0
- javax.json-1.0.4
Thanks