gpt4 book ai didi

java - Payara 5 上的 Spring-Boot Rest Controller 忽略 JAXB 注释

转载 作者:行者123 更新时间:2023-12-02 01:44:06 26 4
gpt4 key购买 nike

我想将 Spring-Boot 应用程序部署到 Payara 5,但遇到问题。我访问了 Spring Initializr 页面,填充了组、工件并添加了 Web 依赖项。为了能够将应用程序部署到 Payara,我删除了对 Tomcat 的依赖项,调整了 @SpringBootApplication 注解的类,以扩展 SpringBootServletInitializer。我创建了非常简单的 RestController,它返回非常简单的 Pojo。

代码如下: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>

应用程序类:

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);
}
}

休息 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");
}
}

波乔:

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;
}
}

当我运行此应用程序时

mvn spring-boot:run

一切都如我所愿,CURL 请求:

curl -k -i -X GET "http://localhost:8080/sample-pojo" -H "accept: application/xml" -H "Content-Type: application/xml"

返回:

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>

但是,当我将此代码部署到 Payara 并执行 CURL 请求时:

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"

我收到回复:

HTTP/2 200 
content-type: application/xml;charset=UTF-8

<SamplePojo><content>Sample pojo</content></SamplePojo>

问题是 - 为什么 JAXB 注释在 Payara 上被忽略?我需要做什么才能让它们工作?

最佳答案

对于遇到类似问题的任何人 - 问题的根本原因与以下事实有关:MappingJackson2HttpMessageConverter 在 Payara 5 上启动,而 Jaxb2RootElementHttpMessageConverter 则没有那里。我通过提供配置找到了问题的解决方案:

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());
}
}

希望它对将来的人有所帮助,对我来说,花了 1.5 天才弄清楚;(

====================================

更新:第一个解决方案确实打开了 Web MVC 并导致不再提供静态文件。我通过提供配置设法找到了最终解决方案:

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())
);
}
}

关于java - Payara 5 上的 Spring-Boot Rest Controller 忽略 JAXB 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53956310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com