0

I found 10 or 20 similar question and I tried many of the answers but none of them seems to work for me..

Here is the angular service code:

app.factory('service',['$http','$q', function($http,$q){
return {
    get: function(m){
        return $http.post('path/getJSON',m).then(
                function(r){
                    console.log(r.data);
                    return r.data;
                }
        );
    },

    getY: function(m){
        return $http({
                method : 'POST',
                url : 'path/getJSON',
                headers: {'Content-Type': 'application/json'},
                data : JSON.stringify(m)

        }).then(
                function(r){
                    console.log(r.data);
                    return r.data;
                }
        );
    },

    getZ: function(m){
        return $http.post('path/getJSON',JSON.stringify(m)).then(
                function(r){
                    console.log(r.data);
                    return r.data;
                }
        );
    }

};
}]);

I tried to call any of these 3 methods in my controller but the result is the same: 415. I also tried with and without the JSON.stringify() function on any of these

The data (m) is built like this:

m = {param : value, param2 : value2 etc}

Here is the controller method in Java which should handle this POST:

@RequestMapping(value="/getJSON", consumes = {"application/json;charset=UTF-8"},
        method=RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public ResponseEntity<List<SmtElse>>getFlights(@RequestBody CustomJSONObj jsonObj){
    HttpHeaders h= new HttpHeaders();
    h.add("Access-Control-Allow-Origin", "*");
    List<SmtElse> list= new ArrayList<SmtElse>();

    System.out.println(jsonObj);
    return new ResponseEntity<List<SmtElse>>(list,h, HttpStatus.OK);
}

CustomJSONObj has only String attributes, getters, setters and constructors. I also tried adding a couple attributes in @RequestMapping or in other form.

Sorry for possible duplicate. I know there are big chances, but I couldn't find any answer for my problem.

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.4</version>
</dependency>
dskfdskjgds
  • 341
  • 3
  • 14
  • can you check if your requests sends `'Accept': 'application/json'` as well as the content type? – Jorg Apr 14 '16 at 00:11
  • I have added it to the 2nd method in service. still not working. I tried removing @RequestBody from spring controller and it works, but I can't get my data – dskfdskjgds Apr 14 '16 at 00:16
  • I used HttpServletRequest getContentType() method and the result is null. on getHeader("Accept") result is application/json – dskfdskjgds Apr 14 '16 at 00:24
  • I am not able to replicate your problem, Its working fine for me. – Viraj Apr 15 '16 at 04:58

1 Answers1

0

You need to annotate the class with either @RestController or @Controller and @ResponseBody.

See JavaDoc and Difference between spring @Controller and @RestController annotation

Community
  • 1
  • 1
dMcNavish
  • 627
  • 6
  • 12