Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use both @Post and @Get on the same method

Tags:

jersey

tomcat7

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.

like image 441
swemon Avatar asked Jun 27 '13 08:06

swemon


People also ask

Can I use GET and POST at the same time?

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".

Can we use GET and POST together in Java?

It allows you to create GET and Post Methods on a single Annotation by generating a new class from it.

How can use both GET and POST method in HTML?

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.

Is POST better than GET?

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.


2 Answers

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.

like image 79
nicou50 Avatar answered Nov 03 '22 12:11

nicou50


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 :)

Edit:

If for some reason the above link stops working, here's what I did:

  • Created a compile-time annotation @RestMethod for class methods.
  • Created a compile-time annotation @RestClass for classes.
  • Create an AnnotationProcessor which generates a new class with Jersey's corresponding annotations and for each method creates a GET and a POST method which callsback to the original method annotated with @RestClass.

All methods annotated with @RestMethod must be static and contained within a class annotated with @RestClass.

Example (TestService.java):

@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();
    }
}
like image 41
Rob Avatar answered Nov 03 '22 14:11

Rob