mark mark - 1 year ago 68
reST (reStructuredText) Question

Restful url through property file

Hello all i have sample restful service below with jersey 2


@XmlRootElement(name = "book")
@XmlType(propOrder = { "id", "name", "author", "price" })
public class Book {

private String id;
private String name;
private String price;
private String author;

public Book() {


public Book(String id, String name, String price, String author) {
super(); = id; = name;
this.price = price; = author;

public String getId() {
return id;

public void setId(String id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

public String getPrice() {
return price;

public void setPrice(String price) {
this.price = price;

public String getAuthor() {
return author;

public void setAuthor(String author) { = author;


My DAOImpl

public class BooksImpl{
static Connection conn = null;
static Statement stmt;
ResultSet rs;

public List<Book> getAllBooks() throws SQLException, ClassNotFoundException, FileNotFoundException {
List<Book> arrBook = new ArrayList<Book>();
rs = stmt.executeQuery(GET_ALL_BOOKS);
while ( {
arrBook.add(new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
return arrBook;


@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
@Consumes({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
public class BookServiceImpl implements BookService {

private BooksImpl booksImpl = new BooksImpl();

public Response getBooks(@QueryParam("format") String format) throws SQLException, ClassNotFoundException, FileNotFoundException {
return Response.status(Status.OK).entity(new GenericEntity<List<Book>>(booksImpl.getAllBooks()) {
}).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8")


So finally i make a get request say in a postman like ::


It's good and i can get the reponse by getting all the books from DB.

my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"



My requriment is for different http methods we have different url end point paths for example

GET : http://localhost:8080/BooksJAXRS/library
POST : http://localhost:8080/BooksJAXRS/library
DELETE : http://localhost:8080/BooksJAXRS/library/1
PUT : http://localhost:8080/BooksJAXRS/library/1

So i make a props file

DELETE= http://localhost:8080/BooksJAXRS/library/1
PUT= http://localhost:8080/BooksJAXRS/library/1

Now i want to capture that restful url and pass it through props file and route it accordingly depending on the method and url

Thank you

Answer Source

You could create 2 mappings on 2 different methods that call the same private method like this:

public class BookServiceImpl implements BookService {

    public Response getBooksByRoot(@QueryParam("format") String format) {
        return getBooks(format);

    public Response getBooksByLibrary(@QueryParam("format") String format) {
        return getBooks(format);

    private Response getBooks(String format) {

    public Response createBook(@QueryParam("name") String name) {

Response Update:

It seems that you want a dynamic mapping, so you need to use a Path with a regular expression.

public class BookServiceImpl implements BookService {

    public Response getBooks(@PathParam("parameter") String parameter) {
        if (properties.getProperty("GET").endsWith(parameter) {
            // code to get the books here
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();

WARNING: The code snippet above is only meant to show the idea, it is not meant to be perfect. For example testing with endsWith could not be enough according to your context.