0

I am trying to consume JSON object sent by a client into a Map(or JSON object). I am using Jersey2.22.1 and by default it is using MOXY. Tried HashMap as shown below but no luck. It gives 415 error - "Unsupported Media Type"

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Task> addTask(HashMap<String,Object> dynamicParam){

Tried with a custom class as well by wrapping a Map. again the same error. Can some one help me and let me know how to handle Map.

@XmlRootElement
public class DynamicFormData {

Map<Object,Object> data;

public Map<Object, Object> getData() {
    return data;
}

public void setData(Map<Object, Object> data) {
    this.data = data;
}

As a temp Solution, I am using below code. But would like to know how to correctly do this with Map

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Task> addTask(String dynamicParam){     
    log.info("addTask Start");
    Gson gson = new Gson();
    Map<String, Object> map = new HashMap<String, Object>();
    map = (Map<String, Object>)gson.fromJson(dynamicParam, map.getClass());
Sanu
  • 17
  • 1
  • 6
  • Check if your client is sending JSON object only. And ask the client to set header Content-Type header as application/json – voucher_wolves Jan 22 '17 at 05:29
  • Yes it is correct only. I was able to change the type as String public List addTask(String dynamicParam){ and it generated below data {"data":{"name":"sanu","age":"42","cars":["Saab","Volvo","BMW"]},"string":"taskDetails"} – Sanu Jan 22 '17 at 05:35
  • MOXy is not great for `Map`s. Have you ever considered using Jackson instead? – cassiomolin Jan 22 '17 at 15:06
  • Maybe this helps, if you don't want to switch to Jackson. http://stackoverflow.com/questions/29322605/how-to-return-a-json-object-from-a-hashmap-with-moxy-and-jersey – FrAn Feb 01 '17 at 17:37
  • Yashpandey @CássioMazzochiMolin and FrAn ..Thanks for all your help!! I switch to Spring REST which uses Jackson and it works fine with HashMap. Thanks for your help!! – Sanu Feb 23 '17 at 15:45
  • Jersey works with Jackson too :) – cassiomolin Feb 23 '17 at 15:46
  • @CássioMazzochiMolin..Got it :) But REST javax.rs ** API had some conflict with WebSphere and was getting some error on start up ..Didn't spend much time for debugging. Also need some MVC for some other part of the application as well.. hence we switched to Spring MVC. Again as you guys suggested I believe the root cause is Moxy and changing it to Jackson should have resolved in Jerssy as well...:) – Sanu Feb 23 '17 at 15:51

2 Answers2

0

Your client does not send Accept: application/json header.

Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • This is my client. I don't think there is any issue with the client. The client was a working code which accept POJO(custom class - working fine). Just reusing it to get as HashMap.
    
    $.ajax({
       dataType: "json",
       url: "http://localhost:8080/DynamicForm/webapi/tasks",
       data: JSON.stringify(pasingObject),
       type: 'POST',
       contentType: "application/json",
       success: function (data){
        alert("Added Sucessfully")
        tasksTable.ajax.reload();
       },
       error: function(e) {
        alert(e.message);
        console.log(e.message);
       }
    
    – Sanu Jan 22 '17 at 14:46
  • Please edit the question with a complete request . Captured from browser or network. Your client could work because server was accepting requests without Accept header. – Basilevs Jan 22 '17 at 19:04
0

Use @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) in combination with javax.ws.rs.core.MultivaluedMap Working example: JS-Client:

function postCalling(){
$.ajax({
    type: 'POST',
    url: "<url>",
    contenttype: "application/json",
    datatype: "json",
    data: {
        'paramOne': 'ONE',
        'paramTwo' : 'TWO'
    },
    success: function (data, status, jqXHR) {
        alert('It worked!: '+JSON.stringify(data));
    },
    error: function (jqXHR, status) {
        alert('didnt work!');

    }
});

Jersey-Code:

@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
@Path("/sample")
 public String getCumGa1Json(MultivaluedMap<String, String> postDataMap) {
    System.out.println(postDataMap.get("paramOne").get(0));
    //prints ONE
    System.out.println(postDataMap.get("paramTwo").get(0));
    //prints TWO
    return toJSon(anObject);
}
Mike Balthasar
  • 173
  • 1
  • 7
  • Thanks I will try this. Currently, I switched to Spring REST (which uses Jackson) and it is working fine with HashMap. I believe it was to do with Moxy used by Jerssy(didn't tried changing this to Jackson) – Sanu Feb 23 '17 at 15:41