Java 从Ruby接受字符串和XML数据

Java 从Ruby接受字符串和XML数据,java,spring,spring-boot,spring-restcontroller,spring-rest,Java,Spring,Spring Boot,Spring Restcontroller,Spring Rest,我想创建一个REST服务器,它接受来自Ruby代码的XML请求和纯文本到不同的控制器中。我试图实现这一点: @SpringBootApplication public class Application extends SpringBootServletInitializer implements WebMvcConfigurer { @Override protected SpringApplicationBuilder configure(SpringApplication

我想创建一个REST服务器,它接受来自Ruby代码的XML请求和纯文本到不同的控制器中。我试图实现这一点:

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    ..............

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof MappingJackson2XmlHttpMessageConverter);
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2XmlHttpMessageConverter(
                ((XmlMapper) createObjectMapper(Jackson2ObjectMapperBuilder.xml()))
                        .enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)));
        converters.add(new MappingJackson2HttpMessageConverter(createObjectMapper(Jackson2ObjectMapperBuilder.json())));
    }

    private ObjectMapper createObjectMapper(Jackson2ObjectMapperBuilder builder) {
        builder.indentOutput(true);
        builder.modules(new JaxbAnnotationModule());
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.defaultUseWrapper(false);
        return builder.build();
    }    
}

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //MyRequestBodyHttpMessageConverter converter = new MyRequestBodyHttpMessageConverter();
        FormHttpMessageConverter converter = new FormHttpMessageConverter();
        //MediaType utf8FormEncoded = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
        //MediaType mediaType = MediaType.APPLICATION_FORM_URLENCODED; maybe UTF-8 is not needed
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
        //converter.setSupportedMediaTypes(Arrays.asList(utf8FormEncoded));
        converters.add(converter);
        MappingJackson2HttpMessageConverter conv1 = new MappingJackson2HttpMessageConverter();
        conv1.getObjectMapper().registerModule(new JaxbAnnotationModule());
        converters.add(conv1);

        MappingJackson2XmlHttpMessageConverter conv = new MappingJackson2XmlHttpMessageConverter();
        // required by jaxb annotations
        conv.getObjectMapper().registerModule(new JaxbAnnotationModule());
        converters.add(conv);
    }
}
XML响应示例:

<?xml version="1.0" encoding="UTF-8"?>
<payment_response>
    <transaction_type>authorize</transaction_type>
    <status>approved</status>
    <unique_id>5f7edd36689f03324f3ef531beacfaae</unique_id>
    <transaction_id>asdsdlddea4sdaasdsdsa4dadasda</transaction_id>
    <code>500</code>
    <amount>101</amount>
    <currency>EUR</currency>
</payment_response>
通知请求示例:

uniqueid=23434&type=sale&status=33
通知响应示例:它应该只返回HTTP状态OK

我使用:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath />
    </parent>
POM配置:

当在WildFly上部署为WAR应用程序时,Spring Boot 应用程序可能需要一些高级JDK反射API来 代理,包含在sun.reflect包中。为此,你的 应用程序需要将jdk.unsupported列为其 MANIFEST.MF文件(请参阅WildFly wiki了解这一点)

-所以我先用这个

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
        <archive>
            <manifestEntries>
                <Dependencies>jdk.unsupported</Dependencies>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

用于日志记录,并从这里向src/main/resources添加了一个

因此,我的基本jboss-deployment-structure.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

从现在起,在Wildfly控制台中,您可以看到发生了什么

我在两个发行版中测试了POC:

仅Servlet分发版 (wildfly-servlet-13.0.0.Final.zip)

这适用于基本的jboss-deployment-structure.xml

应用服务器分发版 (wildfly-13.0.0.Final.zip)

在该服务器中,我必须添加org.reactivestreams模块:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
        <dependencies>
            <module name="org.reactivestreams"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

您必须将此文件放入src/main/webapp/WEB-INF目录

如果您需要使用与application/x-www-form-urlencoded不同的内容类型,可以通过向内容类型注册适当的MessageConverter来实现

在您的情况下,由于请求正文中的querystring,FormHttpMessageConverter将是“正确的”,您必须将您的内容类型指定为MY_OTHER_content_type常量:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FormHttpMessageConverter converter = new FormHttpMessageConverter();
    MediaType utf8FormEncoded = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED, MY_OTHER_CONTENT_TYPE));
    converters.add(converter);
    super.configureMessageConverters(converters);
}
@覆盖

public void configureMessageConverters(列表请注意目标平台/appserver是Wildfly(版本也是)。因此,您可能需要使用Wildfly的classloader来使用捆绑库,而不是Wildfly自己的模块和JAR。更新。有没有关于如何实现此功能的建议?晚上我将尝试在Wildfly中复制。非常感谢。有没有一种方法可以在不使用
内容类型的情况下接收数据:application/x-www-form-urlencoded
?我已经通过gdrive share共享了我的POC应用程序,您可以检查它的配置、pom等。我恐怕使用了标准的FormHttpMessageConverter。我已经使用了。它正在工作,但只在标题中使用了
内容类型:application/x-www-form-urlcoded
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
        <archive>
            <manifestEntries>
                <Dependencies>jdk.unsupported</Dependencies>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
        <dependencies>
            <module name="org.reactivestreams"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FormHttpMessageConverter converter = new FormHttpMessageConverter();
    MediaType utf8FormEncoded = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED, MY_OTHER_CONTENT_TYPE));
    converters.add(converter);
    super.configureMessageConverters(converters);
}