简体   繁体   中英

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();
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM