Reputation: 229
I'm implementing a RESTful Web Service on Java, with Jersey library. I want it receives a Json object and then convert it to a Usuario class (pojo) for inserting into a database. Here is the current code
UsuarioResource.java
package com.tienda.rest.recurso;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.tienda.rest.pojo.Usuario;
import com.tienda.rest.service.UsuarioService;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
/**
* REST Web Service
*
*/
@Path("usuario")
public class UsuarioResource {
@Context
private UriInfo context;
private final UsuarioService service;
private final Gson json;
/**
* Creates a new instance of UsuarioResource
*/
public UsuarioResource() {
this.service = new UsuarioService();
this.json = new Gson();
}
/* Other codes */
@PUT
@Path("registro")
@Consumes(MediaType.APPLICATION_JSON)
public String registrarUsuario(JsonElement usuario) {
System.out.println((usuario != null) + usuario.getAsString());
return usuario.getAsString();
//Usuario nuevo = this.json.fromJson(usuario, Usuario.class);
//return this.service.registrarUsuario(nuevo);
}
}
Usuario.java
package com.tienda.rest.pojo;
import java.io.Serializable;
import java.util.Date;
/**
* Clase que representa un registro de la base de datos, de la tabla
* {@code usuario}.
*/
public class Usuario implements Serializable {
private String usuario;
private String nombre;
private String apellidos;
private Date fechaNacimiento;
private String direccion;
private String telefono;
private String clave;
public Usuario() {
}
public Usuario(String usuario) {
this.usuario = usuario;
}
public Usuario(String usuario, String clave) {
this.usuario = usuario;
this.clave = clave;
}
public Usuario(String usuario, String nombre, String apellidos, Date fechaNacimiento, String direccion, String telefono, String clave) {
this.usuario = usuario;
this.nombre = nombre;
this.apellidos = apellidos;
this.fechaNacimiento = fechaNacimiento;
this.direccion = direccion;
this.telefono = telefono;
this.clave = clave;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public Date getFechaNacimiento() {
return fechaNacimiento;
}
public void setFechaNacimiento(Date fechaNacimiento) {
this.fechaNacimiento = fechaNacimiento;
}
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public String getTelefono() {
return telefono;
}
public void setTelefono(String telefono) {
this.telefono = telefono;
}
public String getClave() {
return clave;
}
public void setClave(String clave) {
this.clave = clave;
}
}
By the moment, this code throws an JsonMappingException
. Ideas?
Upvotes: 0
Views: 1724
Reputation: 16294
Don't use Gson, jersey will work perfectly with JAXB with MOXy provider (default on Glassfish/Payara).
All you have to do is annotate the pojo fields with the right JAXB annotations, then just use the Usuario
type itself as a method parameter.
@PUT
@Path("registro")
@Consumes(MediaType.APPLICATION_JSON)
public String registrarUsuario(Usuario usuario) {
Upvotes: 1
Reputation: 670
I am definitely not familiar with this library, but I'd assume you'll need to tell it how to map the JSON fields to Java attributes. Maybe some sort of annotations?
Upvotes: 0