Home Rest Service method mapping [Java Spring]
Reply: 2

Rest Service method mapping [Java Spring]

Blood Armor
1#
Blood Armor Published in 2018-02-14 11:03:30Z

This is my first try at writting service from scratch. I'm using RestController and Java Spring to create a service which generates pdf based on parameters which are passed when calling the service. Service is called with one parameter, but can be called with two different variables (one is registry number and the other is identificator) and depending on which one of those two is passed, service generates the same JSON but different service is called in background of my program (one call works with IDN and one works with regNum).

So far I have this:

@RequestMapping(value = "/generatePdf/{idn}", method = RequestMethod.GET, produces = "application/pdf")
public String generatePdf(@PathVariable String idn) {
    //logic
}

My question is this. What is the best solution for this problem? Do I make separate method with different name and mapping? Should I create a flag which checks which type od data is sent? Or, something third, feel free to suggest.

Vadym Dudnyk
2#
Vadym Dudnyk Reply to 2018-02-14 11:19:59Z

I would recommend you to create separate method instead of adding the additional flag:

  • API will be more readable and understandable, eg: GET /pdfByIdn/{idn} and GET /pdfByRN/{rn}
  • Easy to add additional cases, without modification of existing methods
  • Its make more sense to use separate service classes to different approaches to generate PDF's
Muthu
3#
Muthu Reply to 2018-02-14 11:47:03Z
@RequestMapping(value = "/generatePdf/{idn}/{rgn}", method = 
RequestMethod.GET, 
produces = "application/pdf")
public String generatePdf(@PathVariable(required = false) String idn, 
@PathVariable(required = false) String rgn) 
{
if(idn.equals(null){
//logic
}else {
//logic
}
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315758 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO