Reputation: 7687
I have two controllers I wrote using JERSEY api. I am able to run both services just fine, but I would like to join the two of them under the same ImageApi controller eliminating the SubmitFileController. Is it possible to have @Get method and @POST in the same controller? How will it handle the different paths?
currently its: server/image/getPictureById
and server/submitFile
(using post)
code:
@Path("/image")
public class ImageApi extends ServiceAPI{
@Path("/getPictureById/{imageId}")
@GET
@Produces("image/png")
public Response getPictureById(@PathParam("imageId") String imageId){}
this image service gets the contextfrom Service API:
public class ServiceAPI {
@Context
private ServletContext context;
public ServletContext getContext() {return context;}
public void setContext(ServletContext context) {this.context = context;}
}
and
@Path("/submitFile")
public class SubmitFileController {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {}
Upvotes: 1
Views: 62
Reputation: 473
Is it possible to have @Get method and @POST in the same controller?
Yes
How will it handle the different paths?
If you have @Path("/image/getPictureById/")
above the GET method, then /image/getPictureById/
gets appended to the @Path annotation of the controller, if the annotation exists. So the path would become server/submitFile/image/getPictureById/
unless you moved the @Path annotation of the controller to the POST method instead (in which case your http API will stay the same).
Note: You can process GET and POST requests at the exact same path, if you wanted to (doesn't seem to apply here).
public class Controller {
@Path("/submitFile")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {}
@Path("/image/getPictureById/{imageId}")
@GET
@Produces("image/png")
public Response getPictureById(@PathParam("imageId") String imageId){}
Upvotes: 1