gpt4 book ai didi

java - 如何验证 REST 中的路径变量

转载 作者:行者123 更新时间:2023-12-01 12:47:06 25 4
gpt4 key购买 nike

我的问题是如果我使用@PathParam,如何验证请求参数。

例如我有两个请求参数,name 和 id

path is localhost:/.../search/namevalue/idvalue

如果用户提交姓名或 ID 为空,我应该发送一条回复,提及姓名为必填项/ID 为必填项。

如果我使用@QueryParam,我可以进行验证,但如果我必须使用路径变量,我不知道该怎么做。

如果我只是使用 http:/localhost:/.../search/namevaluehttp:/localhost:/.../search/idvalue 进行测试或http:/localhost:/.../search/ 它抛出 servlet 异常。

下面是代码,如果我使用 QueryParams 验证工作得很好,请让我知道我使用 pathparam 时的方法

 @Controller
@Path("/customer")
public class CustomerController extends BaseController implements Customer {

@Override
@GET
@Produces({ "application/json", "application/xml" })
@Path("/search/{name}/{id}/")
public Response searchCustomerDetails(
@PathParam("name") String name,
@PathParam("id") Integer id) {

ResponseBuilder response = null;
CustomerValidations validations = (CustomerValidations) getAppContext()
.getBean(CustomerValidations.class);
CustomerResponse customerResponse = new CustomerResponse();
CustomerService customerService = (CustomerService) getAppContext()
.getBean(CustomerService.class);

try {
validations.searchCustomerDetailsValidation(
name, id,customerResponse);

if (customerResponse.getErrors().size() == 0) {
CustomerDetails details = customerService
.searchCustomerDetailsService(name, id);
if (details == null) {
response = Response.status(Response.Status.NO_CONTENT);

} else {
customerResponse.setCustomerDetails(details);
response = Response.status(Response.Status.OK).entity(
customerResponse);
}
} else {

response = Response.status(Response.Status.BAD_REQUEST).entity(
customerResponse);
}
}

catch (Exception e) {
LOGGER.error(e.getMessage());
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR);

}

return response.build();
} }


@Component
@Scope("prototype")
public class CustomerValidations {

public void searchCustomerDetailsValidation(
String name, Integer id,
CustomerResponse customerResponse) {


if (id == null) {

customerResponse.getErrors().add(
new ValidationError("BAD_REQUEST",
""invalid id));
}

if (name== null
|| (name!= null && name
.trim().length() == 0)) {

customerResponse.getErrors().add(
new ValidationError("BAD_REQUEST", "invalid id"));
}
} }

@XmlRootElement
public class CustomerResponse {

private CustomerDetails customerDetails;
private List<ValidationError> errors = new ArrayList<ValidationError>();

//setters and getters }



public class ValidationError {

private String status;
private String message;


public ValidationError() {

}

public ValidationError(String status, String message) {
super();
this.status = status;
this.message = message;
}
//setters and getters }

最佳答案

您收到异常,因为您没有映射到 @Path("/search/{foo}/")@Path("/search/")< 的方法,因此您应该得到默认的 404 响应,因为这些路径并未真正定义。

我不确定您为什么要验证这些“缺失”的请求路径 - 看起来此端点旨在用作查询端点,因此我建议您使用 @RequestParam/query 参数可以更 REST 地描述您正在尝试的搜索。 search/{name}/{id} 路径会建议永久驻留在此 URL 的特定资源,尽管在本例中您是在此 Controller 上查询客户。

我建议您完全删除 /search 路径,并将查询参数映射到客户 Controller 的“根”上,这样您就会得到类似的内容

@Controller
@Path("/customer")
public class CustomerController extends BaseController implements Customer {

@GET
@Produces({"application/json", "application/xml"})
public Response searchCustomerDetails(
@RequestParam("name") String name,
@RequestParam("id") Integer id) {

// Returns response with list of links to /customer/{id} (below)

}


@GET
@Produces({"application/json", "application/xml"})
@Path("/{id}")
public Response getCustomerDetails(@PathVariable("id") String id) {

// GET for specific Customer
}
}

关于java - 如何验证 REST 中的路径变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com