我可以在同一方法上同时使用@Post和@Get吗 [英] Can I use both @Post and @Get on the same method

查看:584
本文介绍了我可以在同一方法上同时使用@Post和@Get吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在

@GET
@POST
@Path("{mode}")
public void paymentFinish(@PathParam("mode") String mode, String s) {
    logger.debug("Enter PayStatus POST");
    logger.debug(mode);
}

即使我这样写,我也会出错.我想要的是获取或发布到sameurl的任何内容,相同的方法有效.是否有可能?现在,我将两种方法分开,一种用于获取,一种用于发布.

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.

推荐答案

不幸的是,为了避免Jersey异常,应该只使用一个. 但是您可以执行以下操作:

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.

请注意,java中用于获取与发布的方法名称必须不同.

Note that method name in java for get vs post need to be different.

这篇关于我可以在同一方法上同时使用@Post和@Get吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆