简体   繁体   中英

@FormParam doesn't work with 'Content-Type': 'application/json'

The client side will send the request in json format. However, it looks like the value can't be got by @FormParam annotation. I tried to change the @Consumes(MediaType.APPLICATION_FORM_URLENCODED) but it didn't work.

Which kind of change shall I make on JAVA side to receive the data correctly?

For example, JSON data is

var postData = {
  'uipath': 'xxx\abc\location1',
  'value': 'Hello World!'
};

The JAX-RS method is

@POST
@Path(value = "/receive")
public Object getValueFromUIPath(@Context UriInfo uriInfo,
            @FormParam("uipath") String uiPath,
            @FormParam("value") String value) {
   ...
}

for your information

  1. use consumes annotaion
  2. json should be double quoted

and coming to the answer , you are creating json object

{
  'uipath': 'xxx\abc\location1',
  'value': 'Hello World!'
}

however you are suppose to consume two string fields, not an object. you have two options, whether consume object have two fields uipath and value , or send simple value rather then json object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM