基于 XML 的 Quarkus REST 服务

huangapple go评论80阅读模式
英文:

XML based rest service in quarkus

问题

我需要将基于SOAP的应用转换为基于Quarkus的REST应用程序。

我需要一个Quarkus REST服务来处理以下请求并生成响应。

请求:

<sum>
 <a>5</a>
 <b>5</b>
</sum> 

响应:

<result>10</result>

有任何指引吗!!

英文:

I need to convert my soap based application into rest based quarkus application.

I need a quarkus rest service to take below request and generate response.

Request :

<sum>
 <a>5</a>
 <b>5</b>
</sum> 

Response :

<result>10</result>

Any pointers !!

答案1

得分: 2

你需要在你的 pom.xml 文件中添加 quarkus-resteasy-jaxbquarkus-resteasy 依赖:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-resteasy-jaxb</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-resteasy</artifactId>
</dependency>

然后创建一个简单的模型类,但是记得要加上 @XmlRootElement 注解:

package com.example;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Fruit {

    public Fruit() {
    }

    public Fruit(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String name;

    public String description;

}

然后通过一个 REST 服务暴露这个模型:

package com.example.controller;

import com.example.Fruit;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path("/fruit")
public class FruitRestController {

    @GET
    @Produces(value = MediaType.APPLICATION_XML)
    public List<Fruit> getFruit() {
        List<Fruit> fruitList = new ArrayList<>();
        fruitList.add(new Fruit("Apple", "Crunchy fruit"));
        fruitList.add(new Fruit("Kiwi", "Delicious fruit"));

        return fruitList;
    }
}

只要设置了 Accepts application/xml 头部,这个 Quarkus REST 控制器将会返回如下的 XML 响应:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<collection>
  <fruit>
    <name>Apple</name>
    <description>Crunchy fruit</description>
  </fruit>
  <fruit>
    <name>Kiwi</name>
    <description>Delicious fruit</description>
  </fruit>
</collection>
英文:

You need to add quarkus-resteasy-jaxb and quarkus-resteasy dependency in your pom.xml file:

&lt;dependency&gt;
    &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
    &lt;artifactId&gt;quarkus-resteasy-jaxb&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
    &lt;artifactId&gt;quarkus-resteasy&lt;/artifactId&gt;
&lt;/dependency&gt;

Then create a simple model class, but remember the @XmlRootElement annotation:

package com.example;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Fruit {

    public Fruit() {
    }

    public Fruit(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String name;

    public String description;

}

And then expose the model via a REST service:

package com.example.controller;

import com.example.Fruit;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path(&quot;/fruit&quot;)
public class FruitRestController {

    @GET
    @Produces(value = MediaType.APPLICATION_XML)
    public List&lt;Fruit&gt; getFruit() {
        List&lt;Fruit&gt; fruitList = new ArrayList&lt;&gt;();
        fruitList.add(new Fruit(&quot;Apple&quot;, &quot;Crunchy fruit&quot;));
        fruitList.add(new Fruit(&quot;Kiwi&quot;, &quot;Delicious fruit&quot;));

        return fruitList;
    }
}

Now as long as you set the Accepts application/xml header this Quarkus rest controller will return a XML response like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;collection&gt;
  &lt;fruit&gt;
    &lt;name&gt;Apple&lt;/name&gt;
    &lt;description&gt;Crunchy fruit&lt;/description&gt;
  &lt;/fruit&gt;
  &lt;fruit&gt;
    &lt;name&gt;Kiwi&lt;/name&gt;
    &lt;description&gt;Delicious fruit&lt;/description&gt;
  &lt;/fruit&gt;
&lt;/collection&gt;

答案2

得分: 1

你可以简单地使用一个SOAP库(例如Apache CXF),然后解析XML。这不会涉及特定的Quarkus集成,而且可能会在生成本机镜像方面遇到困难。

英文:

You can simply use a soap library (e.g Apache CXF) and parse the XML. There will be no particular Quarkus integration, and you will probably have a hard time producing a native image.

答案3

得分: 0

添加以下内容:

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jaxb</artifactId>
      <version>1.13.7.Final</version>
    </dependency>

和这个:

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-mutiny</artifactId>
    </dependency>

它们将使您能够在XML中返回 Uni<>。

英文:

Add this:

    &lt;dependency&gt;
      &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
      &lt;artifactId&gt;quarkus-resteasy-jaxb&lt;/artifactId&gt;
      &lt;version&gt;1.13.7.Final&lt;/version&gt;
    &lt;/dependency&gt;

and this:

    &lt;dependency&gt;
      &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
      &lt;artifactId&gt;quarkus-resteasy-mutiny&lt;/artifactId&gt;
    &lt;/dependency&gt;

They will make it possible for you to return Uni<> in XML.

huangapple
  • 本文由 发表于 2020年9月25日 21:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64064734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定