gpt4 book ai didi

java - 在方法级别而不是类级别的基本 Jax-RS PATH 配置

转载 作者:行者123 更新时间:2023-11-29 07:29:25 25 4
gpt4 key购买 nike

我有一个问题,自从我刚开始使用 JAX-RS 以来很长一段时间都想不通。我们可以用方法而不是类来指定路径吗?我正在尝试运行它,但它不起作用。

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


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

有没有可能有这样的东西:

@Component
@Transactional
public class ImageResource {

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

最佳答案

来自@path 文档:

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

所以类和方法可以注解,但是不能跳过类注解。我建议您使用:

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

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

另外我觉得你很奇怪:

1) 在你的 Controller 类上有@Transactional 注释。我相信它应该放在 imageDao 上。此外,请确保所有方法都需要事务。如果否 - 仅将此注释放置在需要的方法上。

2) 您的 Controller 类称为“ImageResource”。最好将此类称为*Controller。在你的例子中是 ImageController。

关于java - 在方法级别而不是类级别的基本 Jax-RS PATH 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45079022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com