I have a problem with Spring-Boot application that I want to deploy to Payara 5. I have visited Spring Initializr page, I've filled group, artifact, and added Web dependency. To make it possible to deploy application to Payara, I've removed dependencies to Tomcat, I've adjusted @SpringBootApplication annotated class, to extend SpringBootServletInitializer. And I've created very simple RestController that returns very simple Pojo.
Here is the code: pom.xml:
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>rest-payara</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-glassfish</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application class:
package com.sample.restpayara;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class RestPayaraApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RestPayaraApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RestPayaraApplication.class);
}
}
Rest controller:
package com.sample.restpayara;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestApiController {
@GetMapping(value = "/sample-pojo", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public SamplePojo getSamplePojo() {
return new SamplePojo("Sample pojo");
}
}
Pojo:
package com.sample.restpayara;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "PojoRoot")
public class SamplePojo {
@XmlElement(name = "pojoContent")
private String content;
public SamplePojo() {
}
public SamplePojo(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
When I run this application with
mvn spring-boot:run
Everything works like I want, CURL request:
curl -k -i -X GET "http://localhost:8080/sample-pojo" -H "accept: application/xml" -H "Content-Type: application/xml"
Returns:
HTTP/1.1 200
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Fri, 28 Dec 2018 09:26:08 GMT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><PojoRoot><pojoContent>Sample pojo</pojoContent></PojoRoot>
However when I deploy this code to Payara and I do CURL request:
curl -k -i -X GET "https://my-payara-domain.local:8181/rest-payara-0.0.1-SNAPSHOT/sample-pojo" -H "accept: application/xml" -H "Content-Type: application/xml"
i receive response:
HTTP/2 200
content-type: application/xml;charset=UTF-8
<SamplePojo><content>Sample pojo</content></SamplePojo>
And here is the problem - why are JAXB annotations ignored on Payara and what do I have to do to make them work?
For anyone ever fighting with similar problem - the root cause of the issue was related to the fact, that MappingJackson2HttpMessageConverter
was kicking in on Payara 5, while Jaxb2RootElementHttpMessageConverter
was not there.
I found a solution for my problem by providing a configuration:
package com.sample.restpayara;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class JaxbSupportConfiguration extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new Jaxb2RootElementHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
}
}
Hope it helps someone in the future, for me it took 1,5 day to figure it out ;(
=================================
UPDATE: First solution did turn on Web MVC and caused static files not to be served anymore. I've managed to find a final solution by providing configuration:
package com.sample.restpayara;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
@SpringBootApplication
public class RestPayaraApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RestPayaraApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RestPayaraApplication.class);
}
@Bean
public HttpMessageConverters converters() {
return new HttpMessageConverters(true, Arrays.asList(
new MappingJackson2HttpMessageConverter(),
new Jaxb2RootElementHttpMessageConverter())
);
}
}