Tipo de contenido 'application / json; charset = UTF-8' no es compatible, cuando intento mandar JSON para la primavera

LOB:

Cuando envío el paquete de JSON jQuery para la primavera RestController tengo muchos errores:

En primavera:

Resuelta [org.springframework.web.HttpMediaTypeNotSupportedException: Tipo de Contenido 'application / json; charset = UTF-8' no soportado]

en Chrome:

La POST http: // localhost / post 415

(Anónimo) @ main.js: 11

Mi código de jQuery:

$(document).ready(function() {

$('#go').on('click', function() {

    var user = {
        "name" : "Tom",
        "age" : 23
    };

    $.ajax({
        type: 'POST',
        url: "http://localhost/post",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(user),
        dataType: 'json',
        async: true
    });

   });

 });

Mi código de primavera RestController:

@RestController
public class mainController {
@RequestMapping(value = "/post", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Object> postUser(@RequestBody User user){
System.out.println(user);
return new ResponseEntity<>("Success", HttpStatus.OK);
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public User getStr(){
    System.out.println("-------------------------");
    return new User("Tom", 56); //'Get' Check
}

}

Entidad Usuario:

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
  private String name;
  private int age;
}
Code_Mode:

está utilizando mal MediaType es decir, APPLICATION_FORM_URLENCODED_VALUEpara consumir en conttroller resto. Utilice MediaType.APPLICATION_JSON_UTF8_VALUEcomo está de paso solicitud JSON.

@RestController
public class mainController {
@RequestMapping(value = "/post", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_JSON,
        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Object> postUser(@RequestBody User user){
System.out.println(user);
return new ResponseEntity<>("Success", HttpStatus.OK);
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public User getStr(){
    System.out.println("-------------------------");
    return new User("Tom", 56); //'Get' Check
}

}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=98894&siteId=1
Recomendado
Clasificación