Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果未使用@Path变量对API进行注释,CXF如何处理这些API?_Java_Rest_Jax Rs_Cxf - Fatal编程技术网

Java 如果未使用@Path变量对API进行注释,CXF如何处理这些API?

Java 如果未使用@Path变量对API进行注释,CXF如何处理这些API?,java,rest,jax-rs,cxf,Java,Rest,Jax Rs,Cxf,场景1:在我的工作中,我遇到了下面的场景,其中:getText1、getText2、getText3、getText4、getText5、getText6没有@Path注释, 但是当我调用API()时,它总是返回以下结果: { "name" : "Sumit1 Arora", "age" : 21, "address" : "Lakshay1 Arora" } SimpleQAImpl @Service("qaservice") @Path("/testqa") pub

场景1:在我的工作中,我遇到了下面的场景,其中:getText1、getText2、getText3、getText4、getText5、getText6没有@Path注释,

但是当我调用API()时,它总是返回以下结果:

{
    "name" : "Sumit1 Arora",
    "age" : 21,
    "address" : "Lakshay1 Arora"
}
SimpleQAImpl

@Service("qaservice")
@Path("/testqa")
public class SimpleQAImpl {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/simpleqa")
    public Person getText() {
        return new Person("Sumit Arora",21,"Lakshay Arora");
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getText1() {
        return new Person("Sumit1 Arora",21,"Lakshay1 Arora");
    }



    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getText3() {
        return new Person("Sumit3 Arora",21,"Lakshay3 Arora");
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getText4() {
        return new Person("Sumit4 Arora",21,"Lakshay4 Arora");
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getText5() {
        return new Person("Sumit5 Arora",21,"Lakshay5 Arora");
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getText6() {
        return new Person("Sumit6 Arora",21,"Lakshay6 Arora");
    }
}
  • 如果@Path没有像上面那样给出,或者在其他场景中也没有给出,您可以告诉我apachecxf是如何工作的吗

  • 有没有参考资料来理解这些东西


  • 场景2:在这个场景中,API调用顶部没有定义@Path变量,如何从URI调用所有这些API?

    @Service
    @Path("/customer")
    public class CustomerResource {
    
        private final Logger logger = LoggerFactory.getLogger(CustomerResource.class);
    
        @Autowired
        private CustomerService customerService;
    
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        public Response create(Customer customer) {
    
            if(customerService.createCustomer(customer).isPresent()) {
                return Response.ok().build();
            } else
            return Response.status(Response.Status.BAD_REQUEST).entity(new Error(1,"test")).build();
        } 
    
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getAll() {
            logger.debug("Received request to fetch all the customers.");
            List<Customer> customers = customerService.fetchAll();
            GenericEntity<List<Customer>> customerEntities = new GenericEntity<List<Customer>>(customers) {};
            return Response.ok(customerEntities).build();
        }
    
        @PUT
        @Consumes(MediaType.APPLICATION_JSON)
        public Response update(Customer customer) {
            return Response.status(Response.Status.NO_CONTENT).build();
        } 
    }
    
    @服务
    @路径(“/customer”)
    公共类CustomerResource{
    私有最终记录器Logger=LoggerFactory.getLogger(CustomerResource.class);
    @自动连线
    私人客户服务;
    @职位
    @使用(MediaType.APPLICATION_JSON)
    @产生(MediaType.APPLICATION_JSON)
    公共响应创建(客户){
    if(customerService.createCustomer(customer.isPresent()){
    返回Response.ok().build();
    }否则
    返回Response.status(Response.status.BAD_请求).entity(新错误(1,“测试”)).build();
    } 
    @得到
    @产生(MediaType.APPLICATION_JSON)
    公众回应getAll(){
    debug(“接收到获取所有客户的请求”);
    List customers=customerService.fetchAll();
    GenericeEntity customerEntities=新的GenericeEntity(customers){};
    返回Response.ok(customerenties.build();
    }
    @放
    @使用(MediaType.APPLICATION_JSON)
    公共响应更新(客户){
    返回Response.status(Response.status.NO_CONTENT.build();
    } 
    }
    
    此处提供了有关CXF如何选择执行哪种方法的文档:。文档通过查看哪种方法有更多的路径参数或更具体的路径来讨论它更喜欢哪种方法,但是第一个场景中的每种方法都有相同的路径,因此选择了第一种。要区分它们,可以使用路径参数

    第二个场景要求您更改URL使用的HTTP方法,以便:

    • 邮政/客户
    • 获取/客户
    • 出售/客户

    将分别调用不同的方法。

    它离开类上的
    @路径。未注释的方法被视为根资源,因为它们是根资源类uri,而注释的方法被视为子资源,因为它们是根资源的子资源。您永远不应该拥有第一块代码。在泽西岛,应用程序甚至不会启动。资源方法太模糊了。消除歧义的方法是使用不同的制作媒体类型。另一方面,如果应用程序启动了,那么就要抓取哪个应用程序。记住URI与单个资源匹配。因此,除非模板化(例如
    {id}
    ),否则每个资源路径应该只指向一个资源,但可以生成不同的内容类型。因此,在同一条路径上,您应该拥有的唯一变体是来自不同内容类型或不同HTTP方法的变体。您的资源方法两者都没有。所以你需要改变它们。第二个代码段很好,因为它们有不同的HTTP方法