JAX-RS异常:使用资源的GET进行批注,类不被视为有效的资源方法 [英] JAX-RS exception: Annotated with GET of resource, class is not recognized as valid resource method

查看:388
本文介绍了JAX-RS异常:使用资源的GET进行批注,类不被视为有效的资源方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS的平针织实现来进行Web服务。我对这个JAX-RS很新。

I am using the jersey implementation of JAX-RS for the web service. I am very new to this JAX-RS.

我试图在服务中添加一个接受Employee对象的方法,并根据Employee对象返回雇员Id值(这有一个数据库命中)。

I am trying to add a method in the service which accept an Employee object and returns the employee Id based on the Employee object values (there is a DB hit for this).

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

Following the Restful principles, I made the method as @GET and provided the url path as shown below:

@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();

}
}

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

For testing purpose, I wrote this Client:

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();
}

}

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

When I run it, I am getting the error: Method, public javax.ws.rs.core.Response com.rest.EmployeeService.getEmpDetails(com.model.Employee), annotated with GET of resource, class com.rest.EmployeeService, is not recognized as valid resource method.

编辑:

型号:

package com.model;

public class Employee {

public String name;
public String age;

}

请告诉我问题在哪里,我是初学者在此并努力理解这些概念:(

Please let me know where is the issue, I am a beginner in this and struggling to understand the concepts :(

推荐答案

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

JAX-RS cannot automatically convert a @PathParam (which is a string value), into an Employee object. Requirements for objects that can be automatically created from a @PathParam are:


  1. 字符串(事实上,因为数据已经是字符串)

  2. 具有接受(单个)字符串作为参数的构造函数的对象

  3. 具有静态的对象valueOf(String)方法

  1. String (defacto, because the data is already a string)
  2. Objects with a constructor that accepts a (single) string as argument
  3. Objects with a static valueOf(String) method

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

For cases 2 & 3 the object would be required to parse the string data and populate its internal state. This is not normally done (because it forces you to make assumptions about the content type of the data). For your situation (just beginning to learn JAX-RS), its best to just accept the incoming @PathParam data as a String (or Integer, or Long).

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






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


Passing a complex object representation to a REST service in a GET method doesn't make much sense, unless its being used, eg, as a search filter. Based on your feedback, that is what you are trying to do. I've actually done this on a project before (generic implementation of search filters), the one caveat being that you need to strictly define the format of the search data. So, lets define JSON as the accepted format (you can adapt the example to other formats as needed). The 'search object' will be passed to the service as a query parameter called 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)

这篇关于JAX-RS异常:使用资源的GET进行批注,类不被视为有效的资源方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆