'Array null or empty in JSON on Jersey WS
I have a WS implemented with Java Jersey (with Swagger). I need to receive in a parameter in json body, which is an array, a list with values, or a list without any values, or a null (baecause in each one of this cases i need a different behaviour) This is my json:
{
"email": "[email protected]",
"name": "SHELDON COOPER2",
"groups": [17,25]
}
But i need to receive also these json: (With the list empty)
{
"email": "[email protected]",
"name": "SHELDON COOPER2",
"groups": []
}
And with the list as null
{
"email": "[email protected]",
"name": "SHELDON COOPER2",
"groups": null
}
Because for me is different, because if i receive the list without values i need to delete all the elements related with the user and if i receive the list as null i don't do antything.
The problem is with my implementation, the second and third case Java works in the same way (puts the parameter as null)
My code:
@PUT
@Path("/usuweb/{userId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response actu_usuweb(@ApiParam(value = "Identificador del usuario web", required = true) @Size(max=32) @PathParam("userId") String userId
,@ApiParam(value = "Body", required = true) ActualizaUsuarioRequestV1 request) {
OracleConnection db = new OracleConnection();
ResponseDB response = new ResponseDB();
Gson gsonIn = new GsonBuilder().setPrettyPrinting().create();
JsonElement innerObject;
innerObject = gsonIn.toJsonTree(request);
innerObject.getAsJsonObject().addProperty("userId", userId);
response = db.processRequest(getCurrentMethod().toUpperCase(), VERSION, innerObject.toString());
try
{
return Response.status(response.getHttpCode()).entity(response.getJson()).build();
}
catch (Exception e)
{
ErrorResponse errorResponse = new ErrorResponse();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement;
errorResponse.setErrorCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
errorResponse.setErrorMessage("Ha ocurrido un error.");
jsonElement = gson.toJsonTree(errorResponse);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(jsonElement.toString()).build();
}
}
The java Structure:
package structuresV1;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.Size;
public class ActualizaUsuarioRequestV1 {
@ApiModelProperty(value = "") @Size(max=255)
private String email;
@ApiModelProperty(value = "") @Size(max=100)
private String name;
@ApiModelProperty(value = "") @Size(max=10)
private String[] groups;
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGroups(String[] groups) {
this.groups = groups;
}
public String[] getGroups() {
return groups;
}
}
When i convert the request body in json with gson library, the parameter groups doesn't exist:
{
"email":"[email protected]",
"name":"SHELDON COOPER2",
"userId":"dsad"
}
And in the database I can not differentiate in which case I am
Thanks & Regards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
