Sumit Arora
Sumit Arora

Reputation: 5241

How CXF Handles APIs If those are not annotated with the @Path Variable?

Scenario-1 : During my work I encountered below scenario, On which : getText1, getText2,getText3,getText4,getText5,getText6 are without @Path annotations,

But when I call the API (http://localhost:8080/.../testqa/ )it always returns following result :

{
    "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");
    }
}
  1. May you please tell me how Apache CXF works, if @Path not given like the case above or on other scenarios as well?

  2. Is there any reference to understand such stuff?


Scenario-2 : On this scenario, No @Path variable defined on top of API Call, how all of these API would be called from URI ?

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

Upvotes: 2

Views: 237

Answers (1)

fiw
fiw

Reputation: 756

The documentation for how CXF selects which method is executed is here: CXF resource selection overview. The docs talks about which method it prefers by looking at which has more path parameters or more a more specific path but each method in your first scenario has the same path so the first one is chosen. To differentiate between them you could use a path parameter.

The Second scenario requires you to change the HTTP method used with the URL so:

  • POST /customer
  • GET /customer
  • PUT /customer

would each invoke the different methods.

Upvotes: 1

Related Questions