英文:
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-jaxb
和 quarkus-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:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jaxb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
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("/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;
}
}
Now as long as you set the Accepts application/xml
header this Quarkus rest controller will return a XML response like this:
<?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>
答案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:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jaxb</artifactId>
<version>1.13.7.Final</version>
</dependency>
and this:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
</dependency>
They will make it possible for you to return Uni<> in XML.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论