我在每个控制器中都有一个请求映射,如下所示,现在我想从我的应用程序的一个位置设置此配置
这是我的代码:
@RestController(value = "AC1004Controller")
@RequestMapping(value = { "api/v1/accounting"},method = RequestMethod.POST ,consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public class AC1004Controller {
}
我的目标编码是,需要从应用程序的一个位置替换以下代码
@RequestMapping(value = { "api/v1/accounting"},method = RequestMethod.POST ,consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})
答案 0 :(得分:1)
通常,您使用GET,POST等映射控制器的方法。
所以下面应该是配置。
在application.properties中定义属性
api.endpoint.accounting=/api/v1/accounting
下面的控制器应使用不同的2种方法与您的会计控制器进行映射,然后使用控制器方法进行映射。
@RestController(value = "AC1004Controller")
@RequestMapping(value = "${api.endpoint.accounting}")
public class AC1004Controller {
@PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<?> addAccount(@RequestBody Account account) {
}
//for get mapping
@GetMapping
public ResponseEntity<?> getAccount() {
}
}
答案 1 :(得分:0)
您只需将这些配置中的任何一个放在应用程序属性文件(yaml或属性)上。
spring.data.rest.basePath=/api
spring.data.rest.base-path=/api
答案 2 :(得分:0)
您需要在spring.mvc.servlet.path
文件中设置application.properties
属性。
像这样:
spring.mvc.servlet.path=/AC1004Controller