Json Post Unsupported Media Type Spring MVC

1

I am doing POST using ajax data in JSON format to controller in Spring MVC , however the server is refusing such request Unsupported Media Type stating that the type is not supported. Is there a configuration that I have to add to the server to accept a request in json?

Follow the codes:

JSP

var registro = {
    "id_user" : "123",
    "data_hora" :"26-07-2016 11:30:59",
    "tipo" :"programador"
}

$.ajax
({
  url: "/ponto/PublicServlet/gravarRegistros.do",
  type: "POST",
  headers: { 
    'Accept': 'application/json',
    'Content-Type': 'application/json' 
  },
  data: JSON.stringify(registro),
  success: function (result){
        alert(result);
  },
  error: function(xhr, status, error) {
      alert("error: "+error+" / status: "+status);
  }
});

Controller

@RequestMapping(value="/gravarRegistros.do" , method = RequestMethod.POST)
public @ResponseBody String gravarRegistros(@RequestBody Registros registro) throws Exception{
    try {
        status = "Ok";

        System.out.println(registro.getData_hora());
        System.out.println(registro.getTipo());
        System.out.println(registro.getId_user());

    } catch (Exception e) {
        e.printStackTrace();
        status = "Nok";
    }
    return "{\"Status\": \""+status+"\"}";
}

Model

@Entity
@Table(name="registros")
public class Registros {

        @Id  
        @GeneratedValue   
        private int id;

        @NotNull(message="id_user não pode ser vazio!")
        @Column(unique=false,nullable=true)
        private String id_user;

        @NotNull(message="data_hora não pode ser vazio!")
        @Column(unique=false,nullable=true)
        private String data_hora;

        @NotNull(message="tipo não pode ser vazio!")
        @Column(unique=false,nullable=true)
        private String tipo;

Server Response

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
    
asked by anonymous 26.07.2016 / 22:44

1 answer

1

I solved the problem by changing the setting of my spring-context.xml .

Lib: com.fasterxml.jackson.databind.jar

Add in spring-context.xml

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean> 

Controller

@RequestMapping(value="/gravarRegistros.do" , method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String gravarRegistros(@RequestBody Registros registro) throws Exception{
    try {
        status = "Ok";

        System.out.println(registro.getData_hora());
        System.out.println(registro.getTipo());
        System.out.println(registro.getId_user());

    } catch (Exception e) {
        e.printStackTrace();
        status = "Nok";
    }
    return "{\"message\": \""+status+"\"}";
}
    
27.07.2016 / 15:57