orange14
orange14

Reputation: 381

Basic Jax-RS PATH configuration at method level rather than class level

I have a question and cant figure out for a long while since I just started with JAX-RS. Can we specify path with method rather than class. I am trying to run it but it does not work.

@Path("/images")
@Component
@Transactional
public class ImageResource {


    @GET
    public List<Image> getAll(){
        return this.imageDao.findAll();
    }
}

Is it possible to have something like this:

@Component
@Transactional
public class ImageResource {

    @GET
    @Path("/images")
    public List<Image> getAll(){
        return this.imageDao.findAll();
    }
}

Upvotes: 2

Views: 1996

Answers (1)

Ivan Lymar
Ivan Lymar

Reputation: 2293

From @path documentation:

Identifies the URI path that a resource class or class method will serve requests for.

https://docs.oracle.com/javaee/7/api/javax/ws/rs/Path.html

So you can annotate classes and methods, but you can't skip class annotation. I would suggest you using:

@Path("/")
@Component
@Transactional
public class ImageResource {

    @GET
    @Path("/images")
    public List<Image> getAll(){
        return this.imageDao.findAll();
    }
}

Also it's seems strange to me that you:

1) Have @Transactional annotation on your controller class. I believe it should be placed on imageDao. Furthermore be sure that all methods require transactions. If no - place this annotation only on needed methods.

2) Your controller class is called "ImageResource". It's better to call such classes as *Controller. In your case ImageController.

Upvotes: 6

Related Questions