0

We're currently in the process of upgrading our services to Spring Boot 3. One of them was relying on suffix-based content negotatiation (ie GET "/products/id.json" got the json, GET "/products/id.xml" the xml representation).

This was done using the mvc configuration

spring:
  mvc:
    contentnegotiation:
      favor-path-extension: true
    pathmatch:
      matching-strategy: ant-path-matcher
      use-registered-suffix-pattern: true
      use-suffix-pattern: true

use-registered-suffix-pattern was working before, I tried the use-suffix-pattern as it is mentioned here. Both don't work anymore.

That link refers to Spring Boot 2.6 as enabling a legacy feature; I could not find any documentation about this for Spring Boot 3. Checking the WebMvcProperties class which is the container of these properties, they seem to have been removed completely, only the matching-strategy remains.

Also, the ContentNegotiationConfigurer's favorPathExtension is deprecated, and the one in ContentNegotiationManagerFactoryBean states "As there is no replacement for this method, in 5.2.x it is necessary to set it to false. In 5.3 the default changes to false and use of this property becomes unnecessary".

How is this unnecessary when we want to keep our interface downward compatible? What is the intended replacement for this? Do we need to use path matchers and add new get mappings, ie

@GetMapping(value = "/products/{id}.json", produces=JSON)
Product productJson(String id) { return service.product(id); }

@GetMapping(value = "/products/{id}.xml", service.produces=XML)
Product productXml(String id) { return service.product(id); }

@GetMapping(value = "/products/{id}", service.produces={XML, JSON})
Product product(String id) { return service.product(id); }

or am I missing something?

daniu
  • 14,137
  • 4
  • 32
  • 53
  • 1
    Note: The tag [spring-boot-3] is [being discussed on Meta](https://meta.stackoverflow.com/q/425634/10871900). Please do NOT remove it until consensus is reached. – dan1st Jul 20 '23 at 18:19

1 Answers1

0

try the below

@GetMapping(value = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<Product> getProduct(@PathVariable Long id) {
        // Your logic to fetch the product by id
        // Return the ResponseEntity with appropriate content type (JSON or XML)
    } 

yaml:

spring:
  mvc:
    contentnegotiation:
      suffix:
        enabled: true
Toni
  • 3,296
  • 2
  • 13
  • 34
Ranjan
  • 74
  • 6