2

I have a web service made witj Java and Jersey. I want to receive a JSON request and parse the json for savethe values stores on the json on the database.

This is mi web service code:

@Path("companies")
public class Companies {

    @Path("add")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject addCompanies(JSONObject inputJsonObj){

        String input = (String) inputJsonObj.get("company");
        String output = "The input you sent is :" + input;
        JSONObject outputJsonObj = new JSONObject();
        outputJsonObj.put("output", output);

        return outputJsonObj;
    }
}

The client side is made with AngularJS:

$scope.company = "";
    $scope.submit = function(){
        // Writing it to the server
        //      
        var dataObj = {
                company : $scope.company
        };  
        var res = $http.post('http://localhost:8080/WS-Test2/crunchify/companies/add', dataObj);
        res.success(function(data, status, headers, config) {
            $scope.message = data;
            notify("succes");
        });
        res.error(function(data, status, headers, config) {
            //alert( "failure message: " + JSON.stringify({data: data}));
            notify("fail");
        });
    };

This is the error I'm getting when I pass the JSON to the web service:

Status Code:415

And this is the request I am sending:

{"company":"Testing2"}

This is my Network tab:

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
jcobo1
  • 1,065
  • 1
  • 18
  • 34
  • mm it looks strange .. i have done a little research about the error code 415 ..and it looks like the format is not supported..so..are you sure about how you set the json type of your back end api? maybe with something like @Consumes ({MediaType. APPLICATION_JSON, MediaType. APPLICATION_JSON}) – federico scamuzzi Jan 08 '17 at 01:41
  • I tried with @Consumes ({MediaType. APPLICATION_JSON, MediaType. APPLICATION_JSON}) but I'm getting the same error – jcobo1 Jan 08 '17 at 01:46
  • mm looks strange cause Angular $http.post send for default json... so i can guess the problem is in the back end configuration..but i don't know java :-) – federico scamuzzi Jan 08 '17 at 01:47
  • Go to your **browser's developer tools Network tab and click on the respective service call**. Take a **screenshot** and update to your question. – Aravind Jan 08 '17 at 01:56
  • @Aravind I posted – jcobo1 Jan 08 '17 at 02:09
  • @Aravind no I'm sorry – jcobo1 Jan 08 '17 at 02:13
  • @Aravind explain me what could be wrong and I will try to fix it, I need to learn for myself. Thanks for your help. – jcobo1 Jan 08 '17 at 02:18

1 Answers1

1

Without further configuration, JSONObject is not something that Jersey supports. You will just need to work with Strings

public String addCompanies(String json){
    JSONObject inputJsonObj = new JSONObject(json)

    String input = (String) inputJsonObj.get("company");
    String output = "The input you sent is :" + input;
    JSONObject outputJsonObj = new JSONObject();
    outputJsonObj.put("output", output);

    return outputJsonObj.toString();
}

If you really want to use JSONObject you can check out this post.

See Also:

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720