简体   繁体   English

Rest Service方法映射[Java Spring]

[英]Rest Service method mapping [Java Spring]

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. 我正在使用RestController和Java Spring创建一个服务,该服务根据调用该服务时传递的参数生成pdf。 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). 服务使用一个参数调用,但可以使用两个不同的变量调用(一个是注册表编号,另一个是标识符),并且根据传递的两个变量中的哪一个,服务生成相同的JSON,但是在我的程序(一个调用适用于IDN,一个调用适用于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? 我应该创建一个标志来检查发送哪种类型的od数据吗? Or, something third, feel free to suggest. 或者,第三点,随时提出建议。

@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
}
}

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} API将更具可读性和可理解性,例如:GET / pdfByIdn / {idn}和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 对不同的方法使用单独的服务类来生成PDF更为有意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM