基于 XML 的 Quarkus REST 服务

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

XML based rest service in quarkus

问题

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

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

请求:

  1. <sum>
  2. <a>5</a>
  3. <b>5</b>
  4. </sum>

响应:

  1. <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 :

  1. <sum>
  2. <a>5</a>
  3. <b>5</b>
  4. </sum>

Response :

  1. <result>10</result>

Any pointers !!

答案1

得分: 2

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

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-resteasy-jaxb</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.quarkus</groupId>
  7. <artifactId>quarkus-resteasy</artifactId>
  8. </dependency>

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

  1. package com.example;
  2. import javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement
  4. public class Fruit {
  5. public Fruit() {
  6. }
  7. public Fruit(String name, String description) {
  8. this.name = name;
  9. this.description = description;
  10. }
  11. public String name;
  12. public String description;
  13. }

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

  1. package com.example.controller;
  2. import com.example.Fruit;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Path("/fruit")
  10. public class FruitRestController {
  11. @GET
  12. @Produces(value = MediaType.APPLICATION_XML)
  13. public List<Fruit> getFruit() {
  14. List<Fruit> fruitList = new ArrayList<>();
  15. fruitList.add(new Fruit("Apple", "Crunchy fruit"));
  16. fruitList.add(new Fruit("Kiwi", "Delicious fruit"));
  17. return fruitList;
  18. }
  19. }

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

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <collection>
  3. <fruit>
  4. <name>Apple</name>
  5. <description>Crunchy fruit</description>
  6. </fruit>
  7. <fruit>
  8. <name>Kiwi</name>
  9. <description>Delicious fruit</description>
  10. </fruit>
  11. </collection>
英文:

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

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  3. &lt;artifactId&gt;quarkus-resteasy-jaxb&lt;/artifactId&gt;
  4. &lt;/dependency&gt;
  5. &lt;dependency&gt;
  6. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  7. &lt;artifactId&gt;quarkus-resteasy&lt;/artifactId&gt;
  8. &lt;/dependency&gt;

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

  1. package com.example;
  2. import javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement
  4. public class Fruit {
  5. public Fruit() {
  6. }
  7. public Fruit(String name, String description) {
  8. this.name = name;
  9. this.description = description;
  10. }
  11. public String name;
  12. public String description;
  13. }

And then expose the model via a REST service:

  1. package com.example.controller;
  2. import com.example.Fruit;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Path(&quot;/fruit&quot;)
  10. public class FruitRestController {
  11. @GET
  12. @Produces(value = MediaType.APPLICATION_XML)
  13. public List&lt;Fruit&gt; getFruit() {
  14. List&lt;Fruit&gt; fruitList = new ArrayList&lt;&gt;();
  15. fruitList.add(new Fruit(&quot;Apple&quot;, &quot;Crunchy fruit&quot;));
  16. fruitList.add(new Fruit(&quot;Kiwi&quot;, &quot;Delicious fruit&quot;));
  17. return fruitList;
  18. }
  19. }

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  2. &lt;collection&gt;
  3. &lt;fruit&gt;
  4. &lt;name&gt;Apple&lt;/name&gt;
  5. &lt;description&gt;Crunchy fruit&lt;/description&gt;
  6. &lt;/fruit&gt;
  7. &lt;fruit&gt;
  8. &lt;name&gt;Kiwi&lt;/name&gt;
  9. &lt;description&gt;Delicious fruit&lt;/description&gt;
  10. &lt;/fruit&gt;
  11. &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

添加以下内容:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-resteasy-jaxb</artifactId>
  4. <version>1.13.7.Final</version>
  5. </dependency>

和这个:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-resteasy-mutiny</artifactId>
  4. </dependency>

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

英文:

Add this:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  3. &lt;artifactId&gt;quarkus-resteasy-jaxb&lt;/artifactId&gt;
  4. &lt;version&gt;1.13.7.Final&lt;/version&gt;
  5. &lt;/dependency&gt;

and this:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  3. &lt;artifactId&gt;quarkus-resteasy-mutiny&lt;/artifactId&gt;
  4. &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:

确定