0

I have a request mapping in every controller like below, now I want to set this configuration from one place of my applications

Here is my code:

@RestController(value = "AC1004Controller")
@RequestMapping(value = { "api/v1/accounting"},method = RequestMethod.POST ,consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public class AC1004Controller {
}

My target coding is, need to replace the below code from one place of our application

 @RequestMapping(value = { "api/v1/accounting"},method = RequestMethod.POST ,consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE}) 
Shohel
  • 3,886
  • 4
  • 38
  • 75

3 Answers3

2

Generally you map controller's methods with GET, POST, etc ..

so below should be configuration..

  1. Define a property in application.properties

    api.endpoint.accounting=/api/v1/accounting
    
  2. Below controller should mapped with your accounting controller with different-2 methods for post, get to mapped with controller method.

    @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() {
    
       }
    }
    
kj007
  • 6,073
  • 4
  • 29
  • 47
  • getting error to this line api.endpoint.accounting=api/v1/accounting – Shohel Oct 28 '18 at 08:30
  • Did you define it in application.properties? Then where error is coming ?? – kj007 Oct 28 '18 at 08:34
  • @shohel sorry there was typo use slash in starting api.endpoint.accounting=/api/v1/accounting – kj007 Oct 28 '18 at 09:06
  • I am new in spring boot, i did not understand your terms. I defined application.properties into pom.xml – Shohel Oct 28 '18 at 09:21
  • ok, you should have a file application.properties under /src/main/resources where you can define properties and they can be called like I called in request mapping by ${} so define it under path I have given. – kj007 Oct 28 '18 at 09:23
0

You need to set spring.mvc.servlet.path property in application.properties file. Like this: spring.mvc.servlet.path=/AC1004Controller

-1

You just put any of these configurations on application properties file (yaml or properties).

spring.data.rest.basePath=/api  
spring.data.rest.base-path=/api
Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33