Issue
I'm making an API for my web application, which runs on a tomcat server, the plan is to make a CRUD REST API
like Prestashop but i don't know how to add the ids of a table to the url path.
So when you go to customers(http://yourUrl.com/api/customers
) you will get the reply:
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customers>
<customer id="1" xlink:href="http://yourUrl.com/api/customers/1"/>
<customer id="2" xlink:href="http://yourUrl.com/api/customers/2"/>
<customer id="3" xlink:href="http://yourUrl.com/api/customers/3"/>
<customer id="4" xlink:href="http://yourUrl.com/api/customers/4"/>
<customer id="5" xlink:href="http://yourUrl.com/api/customers/5"/>
<customer id="6" xlink:href="http://yourUrl.com/api/customers/6"/>
<customer id="7" xlink:href="http://yourUrl.com/api/customers/7"/>
<customer id="8" xlink:href="http://yourUrl.com/api/customers/8"/>
<customer id="9" xlink:href="http://yourUrl.com/api/customers/9"/>
<customer id="10" xlink:href="http://yourUrl.com/api/customers/10"/>
<customer id="11" xlink:href="http://yourUrl.com/api/customers/11"/>
</customers>
</prestashop>
And then you can go to http://yourUrl.com/api/customers/1
then my plan is to add a servlet with several mappings, I've found out how to ad servlets but not mappings at runtime.
How can i archive this?
Solution
You're on the right track but you don't need to modify the web.xml every time you add a customer. If you need to stay with "pure" servlets then you best bet is to change the mapping in web.xml to something like:
<servlet-mapping>
<servlet-name>CustomerHandler</servlet-name>
<url-pattern>/api/customers/*</url-pattern>
</servlet-mapping>
Then, in the servlet (for example, the doGet())
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String pathInfo = request.getPathInfo();
// process pathinfo
}
Your pathInfo String will be whatever comes after the servlet path. So, for example, if the request is "/api/customers/8" then the pathInfo String will be "/8". You'll have to parse this, handling errors (like if the url is "/api/customers/blah"). If the pathInfo is null then there was nothing provided after "/api/customers" and you can assume this returns the list of customers with ids.
But for information, this is the "old" way. It will work but it requires you to do a bit more work than needed. The "new" way is to use JAX-RS. In this case you can define a service to do what you want. The syntax would be something like:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/api/customers")
public class CustomerProcessor {
@Path("/")
@Produces({ MediaType.APPLICATION_XML })
@GET
public Response getCustomerData() {
// get the customer list - assume that it's a String
String customerXML = ...;
return Response.ok(customerXML).build();
}
@Path("/{customerId}")
@Produces({ MediaType.TEXT_PLAIN }) # not sure of the return type
@GET
public Response getCustomer(@PathParam("customerId") Integer customerId) {
// customerId is set to what is on the url but you didn't parse it
Customer customer = getCustomer(customerId);
return Response.ok(customer).build();
}
}
Note that you have two methods that are mapped to /api/customers - one with and one without a parameter. This allows you to keep your code separate depending on if you've got a customer id or not.
The biggest issue with the second solution is that Tomcat is not supported "out of the box". You'll need to add JAX-RS support to Tomcat or switch to a server that supports it natively (like TomEE, Wildfly, etc.).
To have Tomcat support JAX-RS, there are other posts on that - it's not terrible but it's not "free" either.
Answered By - stdunbar
Answer Checked By - Mary Flores (JavaFixing Volunteer)