天青functions:java how 接受application/xml的内容类型,然后将其转换为pojo

6ioyuze2  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(291)

我一直在寻找关于如何使用java创建azure函数的指南
我也在谷歌上搜索了很久。找不到任何有关如何操作的指南/示例/教程。
默认选项只接受application/json类型并转换为pojo。
我需要的是函数接受application/xml类型的数据。
如果有人能帮我做一个示例代码片段或如何使用它的步骤,我将不胜感激。
非常感谢你。

8dtrkrch

8dtrkrch1#

这是一个使用gson for pojo的简单演示:

import com.google.gson.Gson;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.util.Optional;

public class Function {

    @FunctionName("HttpExample")
    public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
            HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        final String query = request.getQueryParameters().get("name");
        final String body = request.getBody().orElse(query);

        if (body == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Please pass a json with name and id in in the request body").build();
        } else {
            try {
                User user = new Gson().fromJson(body, User.class);
                return request.createResponseBuilder(HttpStatus.OK)
                        .body("Hello " + user.name + ", your id is " + user.id).build();

            } catch (Exception e) {
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("error eccors").build();
            }

        }

    }
}

class User {

    public int id;
    public String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

结果:

更新
如果要接受pojo的xml,请尝试以下代码:

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.io.StringReader;
import java.util.Optional;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Function {

    @FunctionName("HttpExample")
    public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
            HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        final String query = request.getQueryParameters().get("name");
        final String body = request.getBody().orElse(query);

        if (body == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Please pass a json with name and id in in the request body").build();
        } else {
            try {

                JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

                StringReader reader = new StringReader(body);
                User user = (User) unmarshaller.unmarshal(reader);

                return request.createResponseBuilder(HttpStatus.OK)
                        .body("Hello " + user.name + ", your id is " + user.id).build();

            } catch (Exception e) {
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body(e).build();
            }

        }

    }
}

@XmlRootElement(name = "User")
@XmlAccessorType(XmlAccessType.FIELD)
class User {
    @XmlElement(name = "Id")
    public int id;

    @XmlElement(name = "Name")
    public String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

结果:

相关问题