cancel
Showing results for 
Search instead for 
Did you mean: 
RogerioLSantos
Mission Specialist
Mission Specialist
  • 2,186 Views

JAX-RS - JSON Attributes

HI, folks

Is it possible to create a POST web service by getting each JSON attribute in an attribute of the method? (Without creating a POJO class representing this JSON)

For example, I want to send the JSON bellow.

{
    "name":"Smith",
    "email":"smith@smiths.com"
}

and the web service implementation is :

@POST
@Produces(MediaType.APPLICATION_JSON)
public void register(String name, String email) {...}

 

Tks

 

Labels (2)
2 Replies
lribas
Cadet
Cadet
  • 2,177 Views

Hello RogerioLSanto.

This form I believe that is not possible, but you can do different, by example:

{
    "name":"Smith",
    "email":"smith@smiths.com"
}

 

@POST
@Produces(MediaType.APPLICATION_JSON)
public void register(String body) {

JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(body);

}

 

Tags (1)
ramalho
Cadet
Cadet
  • 2,125 Views

You need to wrap it.

e.g:

 

public class Person {
private String name;
private String email;

// gets and sets
}


@@POST @Produces(MediaType.APPLICATION_JSON) public void register(Person person)

System.out.println(person.getName());
System.out.println(person.getEmail());
}

 

Join the discussion
You must log in to join this conversation.