Question

i'm a spring newbie and i'm trying to pass a json object to spring mvc:

POJO rappresentation of json

public class City implements Serializable{

    private String id;  
    private Map<String,Map<String,String>> translates;
    private Map<String,String> providers;

    public City(){}

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public Map<String, Map<String, String>> getTranslates() {
        return translates;
    }

    public void setTranslates(Map<String, Map<String, String>> translates) {
        this.translates = translates;
    }

    public Map<String, String> getProviders() {
        return providers;
    }

    public void setProviders(Map<String, String> providers) {
        this.providers = providers;
    }

}

Controller

@RequestMapping(value="/insert",method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody City insert(@RequestBody City city){
    return city; // JUST FOR TEST
}

pom.xml

...
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.0-rc2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0-rc2</version>
</dependency>
....

If i try with that Json

{
    "id": "Test",
    "translates": {
        "it": {
            "country": "testCountryIT",
            "city": "testCityIT"
        },
        "en": {
            "country": "testCountryEn",
            "city": "testCityEn"
        }
    },
    "providers": {
        "prov1": "YYY",
        "prov2": "XXX"
    }
}

i retrieve that error message:

"NetworkError: 415 Unsupported Media Type

What could be the problem?

Thanks!

Was it helpful?

Solution

It seems you aren't sending the Content-Type header with a value of application/json in the request. Your request needs to contain that header so that Spring can 1) use the appropriate HttpMessageConverter and 2) handle the request with your @RequestMapping method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top