2

I'm learning about JAX-RS and Jersey. I am trying to post data to a URL however I have an issue I do not know to fix:

Form formData = new Form();
formData.param("merchant_id", mPayment.getMerchantId());
formData.param("transaction_id", mPayment.getTransactionId());
formData.param("code", mPayment.getCode());
formData.param("amount", String.valueOf(mPayment.getAmount()));
formData.param("desc", mPayment.getDesc());
formData.param("phone", mPayment.getPhone());

Response response = target.path("process").request()
        .accept(MediaType.APPLICATION_JSON)
        .post(Entity.form(formData));

Now everything works well when it's just a string however the server is expecting a float data type for the field amount however when I try to use it without String.valueOf() I get an error. How do I add params with different data types so I can post?

user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • 1
    Don't worry it's the servers' responsibility to cast into float if required. You can simply convert everything to string and send. Hence, your code is correct. – coding_idiot Aug 10 '17 at 22:58
  • 1
    FYI - the `param` takes only string parameters - https://github.com/jax-rs/api/blob/master/jaxrs-api/src/main/java/javax/ws/rs/core/Form.java as expected. – coding_idiot Aug 10 '17 at 23:03

1 Answers1

1

You cannot maintain the type information across the call to the server. The form data will be transferred as text with the application/x-www-form-urlencoded content type header, this is why the Form class accepts String parameter values (similarly to how you could only enter text values in a browser form).

So your code should be sufficient. At the server side, you can declare the parameter as a float and annotate it with javax.ws.rs.FormParam. If the parameter cannot be cast to the desired (float) type by the Jersey runtime, it will return a 400 BAD REQUEST.

In a nutshell:

  • Keep client code as it is.
  • Use server code similar to:

    import javax.ws.rs.FormParam;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.Response;
    
    @Path("/service")
    public class myService {
    
        @POST
        public Response addOrder(
            @FormParam("merchant_id") String merchant_id,
            @FormParam("amount") float amount
            // more parameters 
            ) {
    
            return Response.ok().build();
        }
    }
    
Kostas Stamos
  • 175
  • 1
  • 3
  • 16
  • So you're saying it's not possible and there is no work around and that the only solution for me will be to cast it to a float on the server? – user3718908x100 Aug 10 '17 at 22:32
  • Yes, but you don't need to do the cast yourself, you can declare the parameter annotated with FormParam as a float. See this code example that takes an int form parameter: http://www.mkyong.com/webservices/jax-rs/jax-rs-formparam-example/ – Kostas Stamos Aug 10 '17 at 22:37
  • But the value will not be coming from a form. – user3718908x100 Aug 10 '17 at 22:45
  • Using the javax.ws.rs.core.Form class is the programmatic equivalent of filling in a browser form, as it would set the application/x-www-form-urlencoded content-type. For the server it's just an incoming request and does not care if it came via a browser or a custom client. – Kostas Stamos Aug 10 '17 at 22:50
  • I'm confused right now. So where do I place the annotation? – user3718908x100 Aug 10 '17 at 22:53