I would like to use both @Post and @Get on the same method like
@GET
@POST
@Path("{mode}")
public void paymentFinish(@PathParam("mode") String mode, String s) {
logger.debug("Enter PayStatus POST");
logger.debug(mode);
}
Even I write like this, I got error. What I want is whatever get or post to the sameurl, the same method works. Is it possible? Now I separate two methods, one for get and one for post.
With Ajax you can send data to a PHP script via GET and POST in the same request. The process is similar to sending data through the POST, it's actually the same, being only applied a "trick".
It allows you to create GET and Post Methods on a single Annotation by generating a new class from it.
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.
GET is less secure than POST because sent data is part of the URL. POST is a little safer than GET because the parameters are stored neither in the browser history nor in the web server logs.
Unfortunately, only one should be used in order to avoid Jersey exception. But you could do something like :
@GET
@Path("{mode}")
public void paymentFinish(@PathParam("mode") String mode, String s) {
commonFunction(mode);
}
@POST
@Path("{mode}")
public void paymentFinishPOST(@PathParam("mode") String mode, String s) {
commonFunction(mode);
}
private void commonFunction(String mode)
{
logger.debug("Enter PayStatus POST");
logger.debug(mode);
}
By doing so, if you want to change inner behavior of your functions, you will only have to change one function.
Note that method name in java for get vs post need to be different.
After searching a lot trying to avoid the solution above, I found nothing....
Then I decided to create a custom annotation so I didn't have to waste time duplicating methods.
Here's the github link: Jersey-Gest
It allows you to create GET and Post Methods on a single Annotation by generating a new class from it.
I hope it helps you the same way it helped me :)
If for some reason the above link stops working, here's what I did:
All methods annotated with @RestMethod must be static and contained within a class annotated with @RestClass.
@RestClass(path = "/wsdl")
public class TestService
{
@RestMethod(path = "/helloGest")
public static String helloGest()
{
return "Hello Gest!";
}
}
Generates something like (TestServiceImpl.java):
@Path("/wsdl")
@Produces("application/xml")
public class TestServiceImpl
{
@GET
@Path("/helloGest")
@Produces(MediaType.APPLICATION_XML)
public String helloGestGet()
{
return TestService.helloGest();
}
@POST
@Path("/helloGest")
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_XML)
public String helloGestPost()
{
return TestService.helloGest();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With