我一直在寻找有关如何制作 azure function using java 的指南
而且我已经用谷歌搜索了很长时间。找不到任何有关如何操作的指南/示例/教程。
默认选项仅接受 application/json 类型并转换为 POJO。
我需要的是该函数接受 application/xml 类型的数据。
如果有人能帮助您制作示例代码片段或有关如何实现它的步骤,我们将不胜感激。
非常感谢。
最佳答案
这是一个使用 Gson 进行 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;
}
}
结果:
关于java - Azure函数:Java How to accept content-type of application/xml then convert it to a POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65429587/