1

I am a newbie to JSON. I wish to ask that how can I convert the following code from @GET to @POST so that I can retrieve the appdataid from user JSON input and not from the link.

AppDataService ads = new AppDataService();
@GET
@Path("/{appdataid}")
@Produces(MediaType.APPLICATION_JSON)
public AppData getAppData(@PathParam("appdataid")long appdataid){
    return ads.getAppData(appdataid);
}

Code for AppDataService

public AppDataService(){
        appdatas.put(1L, new AppData(1,"Success", " ", "123456"));
        appdatas.put(2L, new AppData(2,"Failure", " ", "654321"));
    }
public AppData getAppData(long id){
        return appdatas.get(id);
    }

Instead of user entering http://localhost:8080/gni/webapi/appdata/1 for the result

{
        "id": 1,
        "message": " ",
        "status": "Success",
        "token": "123456"
}

I hope that I can receive the user input with @POST and JSON format. I have tried the following code but not working.

@POST
@Path("/{appdataid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AppData getAppData(@PathParam("appdataid")long appdataid){
    return ads.getAppData(appdataid);
}

Thanks for everyone that willing to help me.

Tan Yih Wei
  • 537
  • 3
  • 17
  • 2
    1. What does *"not working"* mean? It's not a proper problem description. 2. What do you mean by *"@POST so that I can retrieve the appdataid from user JSON input"*? What do either `@POST` or `@GET` have to do with the content of your JSON message? – UnholySheep Mar 27 '17 at 08:29
  • Check out this existing StackOverflow question http://stackoverflow.com/a/8194612/695787 – Jameson Mar 27 '17 at 08:31
  • It does not seem like you're accepting anything else with the @POST-method? You're still using @PathParam as input, and that is it - why do you want this to be @POST-specific? Right now there is nothing that indicates that it is nessecary. Also, there is no JSON-input as far as I can tell? :-). – vegaasen Mar 27 '17 at 08:32
  • Hi. @UnholySheep Thanks for your reply. 1. Not working means when I using the `@POST` method above with input {"id" : 1} above, I can't get the result as my `@GET` method produce. 2. I wish to use `@POST` method to get input like {"id" : 1} from user input. appdataid is the variable name. For the `@POST` or `@GET`, if user enter {"id" : 1} as JSON input with `@POST`, they will retrieve the output as `@GET` above. – Tan Yih Wei Mar 27 '17 at 08:37
  • Hi. @vegaasen. Thanks for your reply also. If I wish to have input like `long appdataid` so I have to remove `@PathParam`? I hope for `@POST` specific because I hope that I can get the output by using input in JSON format and not by URL. My JSON input is `{"id" : 1}' and after I testing it with Postman and it return me with error. – Tan Yih Wei Mar 27 '17 at 08:40
  • Hi. @Jameson Thanks for the link. :) – Tan Yih Wei Mar 27 '17 at 08:42

2 Answers2

1

If you want to POST JSON to your resource, you need to define a POJO for it to deserialise the message body into, for example:

public class AppDataRequest {
    @JsonProperty
    private long id;

    public long getId() {
        return id;
    }
}

The use this type for in the resource:

@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AppData getAppData(AppDataRequest appDataRequest){
    return ads.getAppData(appDataRequest.getId());
}

Note, the @Path annotation no longer has appdataid templated, and the parameter does not have an annotation.


SIDE NOTE

I have to wonder what you are trying to achieve here. The HTTP methods can and should be used to help describe the behaviour of your system. A request to get data should use a GET resource. POST resources should be used for creating new data. Sometimes there are good reasons to break this convention (such as the lack of a message body in a GET request), but there are usually other ways around them. Everything about your example tells my it should be a GET request, but I do not know the specifics of you application, so feel free to ignore this if it does not apply.

Michael Peyper
  • 5,946
  • 1
  • 21
  • 42
  • The only reason I can think of for using a POST in this situation would be security (though arguably unless https is used, POST is only slightly more secure ) – dsp_user Mar 27 '17 at 10:00
  • Hi. @Michael Peyper Thanks for your reply. I trying to achieve that different user will receive the different output based on what they input in the JSON format using post. I will try on your answer. Thanks. – Tan Yih Wei Mar 27 '17 at 11:34
  • Hi. @Michael Peyper. Thanks for your answer. It works as the way I wish for. – Tan Yih Wei Mar 29 '17 at 02:44
  • Hi. @MichaelPeyper It is possible me to have more parameters rather than just ID? – Tan Yih Wei Apr 06 '17 at 01:11
  • @TanYihWei Absolutely! Just add them to the request class in the same way the `id` field was. – Michael Peyper Apr 06 '17 at 06:32
  • @MichaelPeyper I tried to add more parameters to the request class and do it the same way as above but when I reach `public AppData getSAppData(int id, String email, String password){ return appdatas.get(id, email, password); } ` It seem that the .get() function only able to receive object only. So should I like convert the three to object? Or should I modified any part of my HashMap? – Tan Yih Wei Apr 06 '17 at 07:07
  • You will need to change the interface of `AppDataService.get(long id)` to either take in the whole `AppDataRequest` type, or to accept the additional parameters. – Michael Peyper Apr 07 '17 at 02:47
0

I don't think you really need JSON for that. You can use @FormParam for such a simple payload (ID)

@POST
@Path("some_url")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)//optional 
public AppData getAppData(@FormParam("appdataid")long appdataid){
    return ads.getAppData(appdataid);
}

You can call your web service from a console (Jersey assumed)

WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/Your_root_url").build()); 

try{

MultivaluedMap<String, Integer> formData = new MultivaluedHashMap<String, String>();

formData.add("appdataid", 1);

Response res= target.path("some_url").request(). 
post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED));

AppData entity = (AppData) res.readEntity(AppData.class);

//System.out.println(entity);//if toString() is overriden


}catch(Exception e){

System.out.println(e);

return;

}

EDIT: The only reason I see for wanting to use a POST in this situation is securiry (and even that is arguable)

dsp_user
  • 1,625
  • 2
  • 12
  • 21
  • Hi. @dsp_user. Thanks for your reply. Yes. You are right about it, I wish to let the user retrieve the information based on some specific input on JSON format. Thanks for your answer, I will try it. – Tan Yih Wei Mar 27 '17 at 11:30