-1

Hi I am developing a iOS project where I am sending a json array to my REST web services which uses JAVA and Jersey along with Google Gson. I have seen several questions on SO which has similar questions like link but I am not able to get how to approach the solution. here I am sending JSON array to the server which has a structure like

{
    "friendList": [
      {"id": 1, "username": "user1", "name":"person1"},
      {"id": 2, "username": "user2", "name":"person2"},
      {"id": 3, "username": "user3", "name":"person3"},...
    ]
}

Here is my Java Class to consume JSON array

@Path("/FriendsList")
public class RestWebServicesAPI {


     @POST
     @Path("/friends")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_JSON)
     public Friends saveFriedList(Friends friend, @Context HttpServletRequest request) {

        //What to write here?

         return friend;

     }


}
  1. DO I need to create Friends Class and map json objects with the class? please help me with some code.
  2. What role does GSON can play here if I have to use google gson to map the json to my friends class.
  3. How can I save the mapped data in mysql database.

any example or any explanation is welcome. thanks

Community
  • 1
  • 1
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60

3 Answers3

0

your class RestWebServicesAPI must implement the interface MessageBodyReader,with its two methods isReadeable and readFrom, read more about this interface here do not forget to annotate your class with @Provider annotation
More detailed explanation of what you should do here, consult the "Java API for JSON Processing" section, page 43.

mounaim
  • 1,132
  • 7
  • 29
  • 56
0

JacksonJsonProvider.class is used for marshalling and unmarshalling JSON Array to your custom BO (Friends) that can be mapped data in mysql database.

You do need to use GSON for converting your BO (Friends) into JSON Object JacksonJsonProvider does it for you.

The class can be obtained using jackson-all-1.9.11.jar.

Gerard
  • 71
  • 2
  • 6
0

First write on POJO class with JAXB annotations and then write a class JSONMessageBodyReader implementing from MessageBodyReader Interface.And you must and should annotate the class with @Provider

        @Provider
    @Consumes(MediaType.APPLICATION_XML)
    public class JSONMessageBodyReader implements MessageBodyReader<Object> {
        @Override
        public Object readFrom(Class<Object> classType, Type rawType, Annotation[] annotation, MediaType mediaType,
                MultivaluedMap<String, String> requestHeaderMap, InputStream inputStream)
                        throws IOException, WebApplicationException {
            System.out.println("Message Body Reader");
            JAXBContext context = null;
            Unmarshaller unmarshaller = null;
            Object obj = null;
            try {
                context = JAXBContext.newInstance(classType);
                unmarshaller = context.createUnmarshaller();
                obj = unmarshaller.unmarshal(inputStream);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            return obj;
        }

        @Override
        `public boolean isReadable(Class<?`> classType, Type rawType, Annotation[] annotation, MediaType mediaType) {
            if (classType.isAnnotationPresent(XmlRootElement.class)) {
                return true;
            }
            return false;
        }
    }
tech.yenduri
  • 586
  • 5
  • 7