Created
June 20, 2019 22:16
-
-
Save thomasdarimont/be60699c0a49b4784e20b1c96dd072dc to your computer and use it in GitHub Desktop.
Simple example for using custom mime-types with fixed request mappings in Spring REST MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@SpringBootApplication | |
public class App { | |
public static void main(String[] args) { | |
SpringApplication.run(App.class, args); | |
} | |
} | |
/** | |
* <pre> | |
* curl -v -H "accept: application/vnd.acme.resource.overview-v1+json" http://localhost:8080/res | |
* curl -v -H "accept: application/vnd.acme.resource.detail-v1+json" http://localhost:8080/res | |
* | |
* curl -v -H "accept: application/vnd.acme.resource.overview-v2+json" http://localhost:8080/res | |
* curl -v -H "accept: application/vnd.acme.resource.detail-v2+json" http://localhost:8080/res | |
* | |
* curl -v -H "Accept: application/json" http://localhost:8080/res | |
* </pre> | |
* | |
*/ | |
@RestController | |
@RequestMapping("/res") | |
class ResourceControllerV1 { | |
@GetMapping(produces = {"application/vnd.acme.resource.overview-v1+json"}) | |
Object getOverview() { | |
return "Resource V1 Overview"; | |
} | |
@GetMapping(produces = {"application/vnd.acme.resource.detail-v1+json"}) | |
Object getDetail() { | |
return "Resource V1 Detail"; | |
} | |
} | |
@RestController | |
@RequestMapping("/res") | |
class ResourceControllerV2 { | |
@GetMapping(produces = {"application/vnd.acme.resource.overview-v2+json"}) | |
Object getOverview() { | |
return "Resource V2 Overview"; | |
} | |
@GetMapping(produces = {"application/vnd.acme.resource.detail-v2+json", "application/json"}) | |
Object getDetail() { | |
return "Resource V2 Detail"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.1.6.RELEASE</version> | |
<relativePath /> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.github.thomasdarimont.training</groupId> | |
<artifactId>spring-boot-multi-contenttype-mvc-demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<description>Demo project for Spring Boot</description> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment