REST - Send null to Integer parameter

Asked

Viewed 259 times

0

I have the following situation in my REST (Jersey):

URL:

http://localhost:8080/api/listar_log_utilizacao/null

Code:

@GET @Path("/listar_log_utilizacao/{idUsuario: .*}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response listarLogUtilizacao(@PathParam("idUsuario") Integer idUsuario) throws Exception  {
...
}

Integer values are received smoothly, however, in this situation the parameter is optional and when null is set to 404 error...

  • Behold here and here. I believe they are useful to you. Now, I do not understand this null as a parameter.

  • In fact null is originated in javascript and sent to the API via Axios... In the API (JAX-RS) null becomes String and thus gives error in the Integer parameter that receives the value... I solved the problem by creating a custom Integer class, which accepts null, null and '

1 answer

-1

Follows resolution:

@GET @Path("/listar_log_utilizacao/{idUsuario: .*}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response listarLogUtilizacao(@PathParam(“idUsuario”) IntegerParam idUsuario) throws Exception {
…
}

Java class:

public class IntegerParam implements IServer {
private Integer number = null;
public IntegerParam(String number) throws WebApplicationException {
try {
if ((number.equals(“null”)) || (number.isEmpty())) {
return;
}
// Converter String em Integer
if (util.isInteger(number)) {
this.number = util.toInt(number);
return;
}
} catch (Exception e) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Couldn’t parse to Integer: " + e.getMessage()).build());
}
}

public Integer getInteger() {
    return this.number;
}

Browser other questions tagged

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