首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >接受来自Ruby的字符串和XML数据

接受来自Ruby的字符串和XML数据
EN

Stack Overflow用户
提问于 2018-09-05 17:11:28
回答 1查看 79关注 0票数 0

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

代码语言:javascript
复制
@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格式是否正确:

代码语言:javascript
复制
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                                                                  HttpHeaders headers, HttpStatus status, WebRequest request) {
        PaymentTransaction response;
        if (ex.getMessage().contains("Required request body")) {
            response = new PaymentTransaction(PaymentTransaction.Response.failed_response, 350,
                    "Invalid XML message: No XML data received", "XML request parsing failed!");
        } else {
            response = new PaymentTransaction(PaymentTransaction.Response.failed_response, 351,
                    "Invalid XML message format", null);
        }
        return ResponseEntity.badRequest().body(response);
    }
}

控制器类:

代码语言:javascript
复制
@RestController()
public class HomeController {

    @Autowired
    public HomeController(Map<String, MessageProcessor> processors, Map<String, ReconcileProcessor> reconcileProcessors,
            @Qualifier("defaultProcessor") MessageProcessor defaultProcessor,
            AuthenticationService authenticationService, ClientRepository repository,
            @Value("${request.limit}") int requestLimit) {
        // Here I receive XML 
    }

    @GetMapping(value = "/v1/*")
    public String message() {
        return "REST server";
    }

    @PostMapping(value = "/v1/{token}", consumes = { MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
                    MediaType.APPLICATION_JSON_VALUE })
    public PaymentResponse handleMessage(@PathVariable("token") String token,
            @RequestBody PaymentTransaction transaction, HttpServletRequest request) throws Exception {
        // Here I receive XML 
    }

    @PostMapping(value = "/v1/notification")
    public ResponseEntity<String> handleNotifications(@RequestBody Map<String, String> keyValuePairs) {
         // Here I receive key and value in request body
    }

    @PostMapping(value = "/v1/summary/by_date/{token}", consumes = { MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
                    MediaType.APPLICATION_JSON_VALUE })
    public PaymentResponses handleReconcile(@PathVariable("token") String token, @RequestBody Reconcile reconcile,
            HttpServletRequest request) throws Exception {
         // Here I receive XML 
    }

    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    public static class UnauthorizedException extends RuntimeException {
        UnauthorizedException(String message) {
            super(message);
        }
    }
}

如您所见,在某些方法中,我接收XML,而在其他方法中,我接收key=value&.....形式的字符串

如何将Spring配置为同时接受这两种类型?另外,我是否应该将Rest控制器拆分成不同的文件?

编辑:

示例XML请求:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<payment_transaction>
  <transaction_type>authorize</transaction_type>
  <transaction_id>2aeke4geaclv7ml80</transaction_id>
  <amount>1000</amount>
  <currency>USD</currency>
  <card_number>22</card_number>
  <shipping_address>
    <first_name>Name</first_name>    
  </shipping_address>
</payment_transaction>

示例XML响应:

代码语言:javascript
复制
<?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>

通知请求示例:

代码语言:javascript
复制
uniqueid=23434&type=sale&status=33

示例通知响应:它应该只返回HTTP status OK。

我使用:

代码语言:javascript
复制
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath />
    </parent>

Java版本:"10.0.2“2018-07-17和Wildfly 13。

关于我使用的XML生成:

代码语言:javascript
复制
@XmlRootElement(name = "payment_transaction")
public class PaymentTransaction {

    public enum Response {
        failed_response, successful_response
    }

    @XmlElement(name = "transaction_type")
    public String transactionType;
    @XmlElement(name = "transaction_id")
    public String transactionId;
    @XmlElement(name = "usage")

POM配置:https://pastebin.com/zXqYhDH3

EN

回答 1

Stack Overflow用户

发布于 2018-09-06 05:56:27

当作为WAR应用程序部署在WildFly上时,Spring Boot应用程序可能需要一些高级

反射API来进行代理,这些API包含在sun.reflect包中。为此,您的应用程序需要在其MANIFEST.MF文件中将jdk.unsupported作为依赖项列出(有关这一点,请参阅WildFly维基)。

source -所以我首先应用了这个。

代码语言:javascript
复制
<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>

接下来,我修复了日志:我为日志应用了the first comment of the accepted answer here,并从这里向src/ basic logback.xml /resources添加了一个资源。

所以我的基本jboss-deployment-structure.xml如下:

代码语言:javascript
复制
<?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模块:

代码语言:javascript
复制
<?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目录中。

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

在你的例子中,由于请求体中的查询字符串,内容类型将是‘正确的’,并且你必须将你的FormHttpMessageConverter -type指定为MY_OTHER_CONTENT_TYPE常量:

代码语言:javascript
复制
@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);
}

然而,使用的转换器应该能够处理它,所以你必须检查转换器的源代码(我这样做了,对于FormHttpMessageConverter,它应该是这样工作的)我想(或者你可以尝试一下,如果它工作了,就工作了)。有时这并不是那么simple,但我希望它会以这种方式为您工作。

如果你不能解决它,请给我们一个有问题的请求的完整样本,我将尝试让它工作。

到测试

请确保您正在POST方法中使用Content-Type: application/x-www-form-urlencoded头,并且您的参数在请求正文中传递。(我已经使用Boomerang插件进行了测试,在那里,当我为post请求定义请求参数时,它将作为get参数添加到请求URL中。这显然导致了错误的结果。)

我希望这些能帮助你解决问题。如果没有,在你应用了适当的日志设置后,你将能够告诉我们为什么(请更新你的帖子的异常)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52181344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档