简体   繁体   中英

Azure Functions:Java How to accept content-type of application/xml then convert it to a POJO

I have been searching for guides on how to make a azure function using java

Also i've been googling it for a long time. Could not find any guide/example/tutorial on how to do it.

The default option only does accept application/json type and convert to POJO.

The one I need is that the function accepts data of type application/xml.

Would appreciate any help to someone to do a sample code snippet or steps on how to to it.

Thank you very much.

This is a simple demo using 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;
    }

}

Result:

在此处输入图像描述

UPDATE

If you want to accept xml for POJO, try code below:

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;
    }

}

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM