0

This is my Resource class where I want to handle POST operation to get @BeanParam

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.stereotype.Component;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Path("")
@Component
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public class MyResource{

    @POST
    @Path("/create")
    @ApiOperation()
    @ApiResponses()
    public String createProfile(@BeanParam final Person person) {
        // Person handling goes here....
    }
}

public class Person{

    @FormParam("name")
    private String name;

    @FormParam("designation")
    private String designation;

    // getters and setters...
}

From a test:

public void test(){
    final String uri = BASE_URI + "/create";

    // Here I am creating Person and converting it as json
    final String jsonInput = SimplePojoMapper.toJSON(person);
    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    final HttpEntity<String> entity = new HttpEntity<>(jsonInput, headers);
    final ResponseEntity<String> responseEntity 
            = this.restTemplate.postForEntity(uri, entity, String.class);
    LOGGER.info("HTTP RESPONSE " + responseEntity.getStatusCode());
}

But it Person has all the null fields.

Is there any way to handle this with @BeanParam. I didn't want to use @FormParam or MultuvaluedMap for some reason.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Hasan
  • 1
  • 1
  • Why don't you just change the server endpoint to accept JSON? What you are doing is very weird. Right now it takes `application/x-www-form-urlencoded` data but you are trying to send JSON data in the body. You need to figure out what you want to do. If you wan to keep it as accepting `application/x-www-form-urlencoded`, then you need to to use just the `MultivalueMap`. https://stackoverflow.com/a/38388168/2587435 – Paul Samsotha Aug 15 '18 at 18:33
  • I know that if I accept JSON it will work. But I cannot change this endpoint to accept JSON because of some backward compatibility. This endpoint will be called from some legacy application. Moreover in the TEST I am setting content type headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); – Hasan Aug 16 '18 at 09:29
  • Yeah but you're trying to send JSON data? Why? Just setting the content-type doesn't change what the data actually is, which is JSON. The data needs to be in `application/x-www-form-urlencoded` format. JSON looks like `{"json":"data"}` and form data looks like `key1=value1&key2=value2`. Its different. You an either send a string in that format, or you can use a `MultivalueMap`, which will get automatically converted to that format. – Paul Samsotha Aug 16 '18 at 20:47
  • Thanks @Paul, I changed the request to send MultivalueMap. – Hasan Aug 20 '18 at 09:55

1 Answers1

0

Sending the payload as JSON won't help you if your server expects it to be a form. So you can use something like:

Form form = new Form();
form.param("name", "John");
form.param("designation", "Anything");

Client client = ClientBuilder.newClient();
Response response = client.target("http://example.com/foo")
                          .request().post(Entity.form(form));
cassiomolin
  • 124,154
  • 35
  • 280
  • 359