quarkus 各种响应格式例子
所属分类 quarkus
浏览量 698
response 输出格式
json
html 网页模板
纯文本
cookie设置
跳转
自适应输出
import java.net.URI;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.CookieParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
@Path("/hello")
public class HelloResource {
@Inject
Template hello;
@GET
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@QueryParam("name") String name) {
return hello.data("name", name);
}
@GET
@Path("/hello2")
public Response hello2() {
return Response.ok("hello", MediaType.TEXT_PLAIN)
.cookie(new NewCookie("cookie001", "cookie001_" + LocalDateTime.now())).build();
}
@GET
@Path("/hello3")
public Response hello3() {
Map< String, String> map = new HashMap< >();
map.put("now", LocalDateTime.now().toString());
return Response.ok(map, MediaType.APPLICATION_JSON)
.cookie(new NewCookie("cookie002", "cookie002_" + LocalDateTime.now())).build();
}
@GET
@Path("/hello5")
public Response hello5(@QueryParam("name") String name) {
TemplateInstance ti = hello.data("name", name);
return Response.ok(ti, MediaType.TEXT_HTML)
.cookie(new NewCookie("cookie003", "cookie003_" + LocalDateTime.now())).build();
}
@GET
@Path("/redirectToHello")
public Response redirectToHello() {
return Response.temporaryRedirect(URI.create("/hello")).build();
}
@POST
@Path("/contextInfoPost")
// payload 请求体
public Response contextInfoPost(String payload, @Context HttpHeaders headers, @Context UriInfo uriInfo,
@Context Request request)
@GET
@Path("/httpRequestAndResponse")
public Response httpRequestAndResponse(@Context HttpRequest httpRequest, @Context HttpResponse httpResponse) {
可直接注入
HttpRequest httpResponse HttpHeaders
httpRequest.getRemoteAddress()
httpRequest.getRemoteHost()
httpRequest.getHttpMethod()
httpRequest.getMutableHeaders()
org.jboss.resteasy.spi.HttpRequest
org.jboss.resteasy.spi.HttpResponse
完整代码
https://gitee.com/dyyx/hellocode/blob/master/demo/quarkus/qute-quickstart/src/main/java/org/acme/qute/HelloResource.java
curl实用技巧
postman中form-data x-www-form-urlencoded raw binary的区别
postman上传文件
上一篇
下一篇
quarkus 配置读取
Spring 和 quarkus 依赖注入相关注解对比
maven常用命令
jquery 获取 query 参数
Vue简介
Javascript strip 去除首尾指定的字符串