0

I have the following code snippet which I have to use pass data to server

function myFuntion() { 
    var myurl = 'myurl/';
    var mydata = $("#myform").serializeArray();
    $.ajax({
        url: myurl,
        type: 'POST',
        contentType : "application/json; charset=utf-8",
        dataType: 'json',
            data: mydata,

        success: function (data) {
            ...
        }
    });
}

and in server JERSEY code

@POST
@Path("/persistlogs")
public Object myMethod(@Context
    UriInfo uriInfo) {

MultivaluedMap params = uriInfo.getPathParameters();
params.size() // is always null 

Issue is uriInfo is always null

What could be the reason for this?

Jacob
  • 14,463
  • 65
  • 207
  • 320

1 Answers1

1

UriInfo is mainly used to get information form the URI/URL. getPathParameters() return a Map of key value pairs where the key is a string extracted from URI templates in @Path. For instance @Path("{id}") would put the id as a key, and its value as the key value.

That being said, you don't want any information from the URI. What you want is the request body content. With application/json, generally we can use POJOs to model the incoming data (as described here). You will need to make sure you have a JSON provider to handle the JSON to POJO deserialization. For help with that, I would need to know what Jersey version you are using (you can comment below if you need help).

Just to see the data coming in (without POJO conversion), you can just have a String parameter, and the deserialization will pass the JSON as a String to the method

@POST
@Path("/persistlogs")
@Consumes("application/json")
public Object myMethod(String jsonData) {
    System.out.println(jsonData);
}

As for sending the data, I would avoid using the .serializArray() for the form. You are going to be left with JSON like

[
  {
    "name": "...",
    "value": "..."
  },
  {
    "name": "...",
    "value": "..."
  }
]

It's not very pretty to work with, as you will need something like Pair class, which really has no semantic value. Better to just create a POJO that maps a Javascript model object to a Java object. This is also described in the first link.


UPDATE

for Jersey (1.x) JSON support, add the following Maven dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.17.1</version>
</dependency>

Then in your web.xml, configure the JSON support.

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I am using Jersey 1,1,7 version. That being said, could you provide with POJO approach so that I could use @Context UriInfo as I find it convenient when number of parameters are more. Thanks – Jacob Nov 30 '15 at 15:41
  • You missed the point of my answer. UriInfo is to obtain data from the URI. your data is not coming in the URI. It is in the request body, where it _should_ be. See the link I provided for help with making POJOs on Java and Javascript side. I will update my most with the Jersey provider information. – Paul Samsotha Nov 30 '15 at 15:49
  • If you want to use `application/x-www-form-urlencoded` instead of `application/json`, then change the `@Consumes` to `application/x-www-form-urlencoded`, and for the method parameter, use `Form form`. You can get the map with `asMap()`. On the client side, take out the `contentType`, and use `serialze()` instead of `serializeArray()` – Paul Samsotha Nov 30 '15 at 15:56