Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
Spring rest json post null values

I have a Spring rest endpoint doing a simple hello app. It should accept a {"name":"something"} and return "Hello, something".

My controller is:

@RestController
public class GreetingController { 

    private static final String template = "Hello, %s!";

    @RequestMapping(value="/greeting", method=RequestMethod.POST)
    public String greeting(Person person) {
        return String.format(template, person.getName());
    }

}

Person:

public class Person {

    private String name;

    public Person() {
        this.name = "World";
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

When I make a request to the service like

curl -X POST -d '{"name": "something"}' http://localhost:8081/testapp/greeting

I get

Hello, World!

Looks like it isn't deserializing the json into the Person object properly. It's using the default constructor and then not setting the name. I found this: How to create a POST request in REST to accept a JSON input? so I tried adding an @RequestBody on the controller but that causes some error about "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported". I see that is covered here: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap which suggests removing the @RequestBody

I have tried removing the default constructor which it doesn't like either.

This question covers null values REST webservice using Spring MVC returning null while posting JSON but it suggests adding @RequestBody but that conflicts with above...

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You must set the @RequestBody to tell to Spring what should be use to set your personparam.

 public Greeting greeting(@RequestBody Person person) {
    return new Greeting(counter.incrementAndGet(), String.format(template, person.getName()));
} 
about 4 years ago · Santiago Trujillo Denunciar

0

You must set 'produces' with @RequestMapping(value="/greeting", method=RequestMethod.POST)

use below code

@RequestMapping(value="/greeting", method=RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
 public String greeting(@RequestBody Person person) {
        return String.format(template, person.getName());
    }
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda