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;
}
}
1条答案
按热度按时间8dtrkrch1#
这是一个使用gson for pojo的简单演示:
结果:
更新
如果要接受pojo的xml,请尝试以下代码:
结果: