gpt4 book ai didi

java - JAX-RS异常: Annotated with GET of resource,类未被识别为有效的资源方法

转载 作者:行者123 更新时间:2023-12-02 03:55:15 24 4
gpt4 key购买 nike

我正在将 JAX-RS 的 Jersey 实现用于 Web 服务。我对这个 JAX-RS 很陌生。

我正在尝试在服务中添加一个方法,该方法接受 Employee 对象并根据 Employee 对象值返回员工 ID(为此有一个数据库命中)。

遵循Restful原则,我将方法设置为@GET,并提供了如下所示的url路径:

@Path("/EmployeeDetails")
public class EmployeeService {
@GET
@Path("/emp/{param}")
public Response getEmpDetails(@PathParam("param") Employee empDetails) {

//Get the employee details, get the db values and return the Employee Id.

return Response.status(200).entity("returnEmployeeId").build();

}
}

出于测试目的,我编写了这个客户端:

public class ServiceClient {

public static void main(String[] args) {

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());

Employee emp = new Employee();
emp.name = "Junk Name";
emp.age = "20";
System.out.println(service.path("rest").path("emp/" + emp).accept(MediaType.TEXT_PLAIN).get(String.class));

}

private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8045/AppName").build();
}

}

当我运行它时,我收到错误:方法,公共(public) javax.ws.rs.core.Response com.rest.EmployeeService.getEmpDetails(com.model.Employee),用 GET 资源注释, com.rest.EmployeeService 类未被识别为有效的资源方法。

编辑:

型号:

package com.model;

public class Employee {

public String name;
public String age;

}

请让我知道问题出在哪里,我是这方面的初学者,并且很难理解这些概念:(

最佳答案

JAX-RS 无法自动将 @PathParam(这是一个字符串值)转换为 Employee 对象。对于可以从 @PathParam 自动创建的对象的要求是:

  1. 字符串(事实上,因为数据已经是字符串)
  2. 具有接受(单个)字符串作为参数的构造函数的对象
  3. 具有静态 valueOf(String) 方法的对象

对于情况 2 和 3,对象需要解析字符串数据并填充其内部状态。通常不会这样做(因为它迫使您对数据的内容类型做出假设)。对于您的情况(刚刚开始学习 JAX-RS),最好只接受传入的 @PathParam 数据作为字符串(或整数或长整型)。

@GET
@Path("/emp/{id}")
public Response getEmpDetails(@PathParam("id") String empId) {
return Response.status(200).entity(empId).build();
}
<小时/>

在 GET 方法中将复杂的对象表示传递给 REST 服务没有多大意义,除非它被用作搜索过滤器等。根据您的反馈,这就是您正在尝试做的事情。实际上,我之前已经在一个项目中完成了此操作(搜索过滤器的通用实现),但需要注意的是,您需要严格定义搜索数据的格式。因此,让我们将 JSON 定义为可接受的格式(您可以根据需要将该示例调整为其他格式)。 “搜索对象”将作为名为 filter 的查询参数传递给服务。

@GET
@Path("/emp")
public Response getEmployees(@QueryParam("filter") String filter) {
// The filter needs to be converted to an Employee object. Use your
// favorite JSON library to convert. I will illustrate the conversion
// with Jackson, since it ships with Jersey
final Employee empTemplate = new ObjectMapper().readValue(filter, Employee.class);

// Do your database search, etc, etc
final String id = getMatchingId(empTemplate);

// return an appropriate response
return Response.status(200).entity(id).build();
}

在您的客户端类中:

final String json = new ObjectMapper().writeValueAsString(emp);
service
.path("rest")
.path("emp")
.queryParam("filter", json)
.accept(emp, MediaType.TEXT_PLAIN)
.get(String.class)

关于java - JAX-RS异常: Annotated with GET of resource,类未被识别为有效的资源方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15024408/

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