Wysłanie prostego obiektu w JSON za pomocą POST - gdzie jest błąd?

0

Zaczynam poznawać springa. Chcę wysłać prosty obiekt w json za pomocą post. Przewertowałem mnóstwo stron i nie mogę dojść gdzie leży błąd. Wysyłam JSON za pomocą RESTClient w FF.

Content-Type: application/json
{imie:"Jan", nazwisko:"Nowak", email:"[email protected]"}

Dostaje odpowiedź:

Status Code: 415 
Content-Language: en
Content-Length: 1091
Content-Type: text/html;charset=utf-8
Date: Wed, 05 Apr 2017 12:33:38 GMT

W logach odkłada się:

DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing POST request for [/dkowalski/user]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /user
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String pl.dr.dkowalski.domain.UserController.post(pl.dr.dkowalski.domain.User,org.springframework.ui.Model)]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public java.lang.String pl.dr.dkowalski.domain.UserController.post(pl.dr.dkowalski.domain.User,org.springframework.ui.Model)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public java.lang.String pl.dr.dkowalski.domain.UserController.post(pl.dr.dkowalski.domain.User,org.springframework.ui.Model)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String pl.dr.dkowalski.domain.UserController.post(pl.dr.dkowalski.domain.User,org.springframework.ui.Model)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
DEBUG: org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request

Metoda kontrolera:

         @RequestMapping(value="/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String post(@RequestBody User user, Model model) {    
   
    	StringBuilder reponseData = new StringBuilder();
    	System.out.println("tsts");
    	System.out.println(user.getImie().toString());
        System.out.println(user.getNazwisko().toString());
        System.out.println(user.getEmail().toString());
        return "hello";
    }

Klasa User to same pola settery i gettery. Do zależności projektu dodałem jackson-core, jackson-databind, jackson-annotations. Czy gdzieś coś jeszcze mogłem pominąć?

0

A jesteś pewien ze ten twój klient wysyła JSONa a nie text jednak?

0

Spróbuj tak

@RequestMapping(value="/user", method = RequestMethod.POST, produces = "application/json")
0

Problem był w starym springu. Utworzyłem projekt springa w STS z starym springiem jeszcze w wersji 3.7.x. Zmieniłem na aktualną wersję springa w maven i zaktualizowałem projekt. Problemem był jeszcze json. Wysyłałem go w złej formie, powinien wyglądać tak (brak cudzysłowów):

{ "imie":"Jan", 
"nazwisko":"Kowalski", 
"email":"[email protected]" }

1 użytkowników online, w tym zalogowanych: 0, gości: 1