spring:
datasource:
username: ***
password: ***
url: jdbc:postgresql://localhost:5432/***
data:
rest:
base-path: api
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'com.google.cloud.tools.jib' version '1.8.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
implementation 'com.fasterxml.woodstox:woodstox-core:6.0.3'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'
// testImplementation 'org.testcontainers:postgresql:1.12.3'
testImplementation 'org.springframework.cloud:spring-cloud-starter:2.1.3.RELEASE'
testImplementation 'com.playtika.testcontainers:embedded-postgresql:1.26'
// testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
// testImplementation('com.h2database:h2:1.4.199')
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}
test {
useJUnitPlatform()
}
package com.example.gradlexml;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class GradleXmlApplication {
public static void main(String[] args) {
SpringApplication.run(GradleXmlApplication.class, args);
}
}
@BasePathAwareController
@RestController
@RequestMapping("/v1/test")
class MyController {
@GetMapping
public MyData get() {
return new MyData("123");
}
}
@Data
@AllArgsConstructor
class MyData {
private String x;
}
If one executes:
http -v :8080/api/v1/test Accept:application/json
then it's ok. But as for XML:
http -v :8080/api/v1/test Accept:application/xml
SB returns 406 with exception in console:
2020-03-27 13:35:47.614 WARN 19356 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
But if the @BasePathAwareController annotation is commented out (and url is prefixed with /api):
...
//@BasePathAwareController
@RestController
@RequestMapping("/api/v1/test")
class MyController {
...
Then everything works as expected.
Comment From: wilkinsona
The behaviour that you are seeing is to be expected as Spring Data REST only supports JSON at this time. When you add @BasePathAwareController
, you are asking Spring Data REST to handle requests to MyController
rather than using Spring MVC's general request handling. As a result, MyController
can no longer produce XML responses.