Json Post Unsupported Media Type Spring MVC

Asked

Viewed 584 times

1

I’m performing a POST using ajax data format JSON for a controller in Spring MVC, but the server is refusing such a request Unsupported Media Type stating that the type is not supported. Have some configuration I have to add to the server accept a request in json?

Follows 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.

1 answer

1


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

Lib:com.fasterxml.jackson.databind.jar

Add to 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+"\"}";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.