如何使用日期作为 Quarkus 中的 @PathParam 在 REST API 中使用 GET METHOD 检索资源(对象)
How to retrieve a resource (object) with GET METHOD in REST API by using date as a @PathParam in Quarkus
我是编程新手,我正在尝试构建一个小型饮食跟踪应用程序。我使用 quarkus RESTeasy JAX-RS 和 java 构建我的 REST API。用户应该能够添加他在一周中的几天吃的食物。
用户必须能够根据日期检索他吃过的食物。我的问题是我无法根据日期取回食物。当我使用时间戳:“2021-06-10T08:44:45.9328079Z[UTC]”作为 GET 方法终点的输入日期时,我在邮递员中收到 400 BAD REQUEST。当我根据 userId 检索食物时,它工作正常。
这是我使用 GET 和 POST 方法的代码:
@Path("/食物")
public class 控制器 {
public static ArrayList<Object> foods = new ArrayList<>();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllFood(){
return Response.ok(foods).build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addFood() throws ParseException {
foods.add(new Food("Garlic", 30, Timestamp.from(Instant.now()), 1));
foods.add(new Food("Onions", 20, Timestamp.from(Instant.now()), 2));
return Response.ok(foods).build();
}
@Path("{userId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public static Response getFoodById(@PathParam("userId") int userId){
for (Object food : foods){
if (((Food)food).getUserId()==(userId)){
return Response.ok(food).build();
}
}
return null;
}
@Path("/second/{time}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFoodByDate(@PathParam("time") Timestamp time){
for (Object food : foods){
if (((Food)food).getTime().toString().equals(time.toString())){
return Response.ok(food).build();
}
}
return null;
}
}
这是食物 Class:
package org.acme;
进口java.sql.Timestamp;
public class 食物 {
private String foodType;
private int portion;
private Timestamp time;
private int userId;
public Food(){}
public Food(String foodType, int portion, Timestamp time, int userId){
this.foodType = foodType;
this.portion = portion;
this.time = time;
this.userId = userId;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Override
public String toString(){
return foodType + ", " + portion + ", " + time + ", " + userId;
}
}
好的 - Postman 抛出的错误是 400 bad request 是因为你输入的参数不正确。
示例代码从 @PathParam
更改为 @RequestParam
@Path("/get/second/")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFoodByDate(@RequestParam("time") String dateString) throws ParseException{
ArrayList<Food> foods = new ArrayList<>();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date requestedDate = sdf.parse(dateString);
for (Object food : foods){
if (((Food)food).getTime().equals(requestedDate)){
return Response.ok(food).build();
}
}
return null;
}
在这个例子中,我更改了你的时间戳参数,因为 java.sql.Timestamp 是 java.util.Date 的薄包装,允许 JDBC API 将其识别为 [=36] =] TIMESTAMP 值,因此在本例中不建议使用时间戳。
重构食物示例 class。
public class Food {
private String foodType;
private int portion;
private Date time;
private int userId;
public Food(){}
public Food(String foodType, int portion, Date time, int userId){
this.foodType = foodType;
this.portion = portion;
this.time = time;
this.userId = userId;
} ... //getter and setter
将食物添加到数组列表的示例代码。
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Food food1 = new Food("Garlic", 10, sdf.parse("2020-05-01"), 1);
Food food2 = new Food("Onion", 20, sdf.parse("2020-05-02"), 1);
Food food3 = new Food("Potato", 30, sdf.parse("2020-05-03"), 1);
Food food4 = new Food("Tomato", 40, sdf.parse("2020-05-04"), 1);
ArrayList<Food> foods = new ArrayList<>();
foods.add(food1);
foods.add(food2);
foods.add(food3);
foods.add(food4);
我测试的完整源代码:
package org.example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2020-05-01"; //mocking request date from the @RequestParam
Date requestedDate = sdf.parse(dateString);
Food food1 = new Food("Garlic", 10, sdf.parse("2020-05-01"), 1);
Food food2 = new Food("Onion", 20, sdf.parse("2020-05-02"), 1);
Food food3 = new Food("Potato", 30, sdf.parse("2020-05-03"), 1);
Food food4 = new Food("Tomato", 40, sdf.parse("2020-05-04"), 1);
ArrayList<Food> foods = new ArrayList<>();
foods.add(food1);
foods.add(food2);
foods.add(food3);
foods.add(food4);
for (Object food : foods){
if (((Food)food).getTime().equals(requestedDate)){
System.out.print(food);
}
}
}
}
输出:Garlic, 10, Fri May 01 00:00:00 SGT 2020, 1
我是编程新手,我正在尝试构建一个小型饮食跟踪应用程序。我使用 quarkus RESTeasy JAX-RS 和 java 构建我的 REST API。用户应该能够添加他在一周中的几天吃的食物。
用户必须能够根据日期检索他吃过的食物。我的问题是我无法根据日期取回食物。当我使用时间戳:“2021-06-10T08:44:45.9328079Z[UTC]”作为 GET 方法终点的输入日期时,我在邮递员中收到 400 BAD REQUEST。当我根据 userId 检索食物时,它工作正常。
这是我使用 GET 和 POST 方法的代码:
@Path("/食物") public class 控制器 {
public static ArrayList<Object> foods = new ArrayList<>();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllFood(){
return Response.ok(foods).build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addFood() throws ParseException {
foods.add(new Food("Garlic", 30, Timestamp.from(Instant.now()), 1));
foods.add(new Food("Onions", 20, Timestamp.from(Instant.now()), 2));
return Response.ok(foods).build();
}
@Path("{userId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public static Response getFoodById(@PathParam("userId") int userId){
for (Object food : foods){
if (((Food)food).getUserId()==(userId)){
return Response.ok(food).build();
}
}
return null;
}
@Path("/second/{time}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFoodByDate(@PathParam("time") Timestamp time){
for (Object food : foods){
if (((Food)food).getTime().toString().equals(time.toString())){
return Response.ok(food).build();
}
}
return null;
}
}
这是食物 Class:
package org.acme;
进口java.sql.Timestamp;
public class 食物 {
private String foodType;
private int portion;
private Timestamp time;
private int userId;
public Food(){}
public Food(String foodType, int portion, Timestamp time, int userId){
this.foodType = foodType;
this.portion = portion;
this.time = time;
this.userId = userId;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Override
public String toString(){
return foodType + ", " + portion + ", " + time + ", " + userId;
}
}
好的 - Postman 抛出的错误是 400 bad request 是因为你输入的参数不正确。
示例代码从 @PathParam
更改为 @RequestParam
@Path("/get/second/")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFoodByDate(@RequestParam("time") String dateString) throws ParseException{
ArrayList<Food> foods = new ArrayList<>();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date requestedDate = sdf.parse(dateString);
for (Object food : foods){
if (((Food)food).getTime().equals(requestedDate)){
return Response.ok(food).build();
}
}
return null;
}
在这个例子中,我更改了你的时间戳参数,因为 java.sql.Timestamp 是 java.util.Date 的薄包装,允许 JDBC API 将其识别为 [=36] =] TIMESTAMP 值,因此在本例中不建议使用时间戳。
重构食物示例 class。
public class Food {
private String foodType;
private int portion;
private Date time;
private int userId;
public Food(){}
public Food(String foodType, int portion, Date time, int userId){
this.foodType = foodType;
this.portion = portion;
this.time = time;
this.userId = userId;
} ... //getter and setter
将食物添加到数组列表的示例代码。
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Food food1 = new Food("Garlic", 10, sdf.parse("2020-05-01"), 1);
Food food2 = new Food("Onion", 20, sdf.parse("2020-05-02"), 1);
Food food3 = new Food("Potato", 30, sdf.parse("2020-05-03"), 1);
Food food4 = new Food("Tomato", 40, sdf.parse("2020-05-04"), 1);
ArrayList<Food> foods = new ArrayList<>();
foods.add(food1);
foods.add(food2);
foods.add(food3);
foods.add(food4);
我测试的完整源代码:
package org.example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2020-05-01"; //mocking request date from the @RequestParam
Date requestedDate = sdf.parse(dateString);
Food food1 = new Food("Garlic", 10, sdf.parse("2020-05-01"), 1);
Food food2 = new Food("Onion", 20, sdf.parse("2020-05-02"), 1);
Food food3 = new Food("Potato", 30, sdf.parse("2020-05-03"), 1);
Food food4 = new Food("Tomato", 40, sdf.parse("2020-05-04"), 1);
ArrayList<Food> foods = new ArrayList<>();
foods.add(food1);
foods.add(food2);
foods.add(food3);
foods.add(food4);
for (Object food : foods){
if (((Food)food).getTime().equals(requestedDate)){
System.out.print(food);
}
}
}
}
输出:Garlic, 10, Fri May 01 00:00:00 SGT 2020, 1