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.