stack man stack man - 1 year ago 65
Java Question

Any hint to simplify this POST? (Java)

I implemented this POST operation in Jax-RS and it is working fine, however I am sure I can send the body in other way somehow. Any hint to simplify it? (By receiving an object for instance?)


public Response updateUser(@FormParam("user") String name,
@FormParam("surname") String surname,
@FormParam("address") String address) throws UnknownHostException {
User user;
user = new CorporateUser(name, surname, address);
return Response.ok(user).build();

Answer Source

Add a JSON provider like Jackson to your project.

@Consumes(value = { MediaType.APPLICATION_JSON })
@Produces(value = { MediaType.APPLICATION_JSON })
public Response updateUser(NewCorporateUserRequest req) throws UnknownHostException {
      User user;
      user = new CorporateUser(req.getName(), req.getSurname, req.getAddress); 
      return Response.ok().entity(user).type(MediaType.APPLICATION_JSON).build();

public class NewCorporateUserRequest implements {

    private String name;
    private String surname;
    private String address;

    ... Getters and Setters.