可以在REST Java WS中使用Response中的GenericEntity发送吗?

时间:2017-04-04 17:09:51

标签: java web-services rest jersey

我尝试在Response中发送一些对象,但我没有成功。我正在使用Glassfish Jersey库。

来自服务器端服务的代码片段:

@Override
@PUT
@Path("/employees")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response addEmployee(@FormParam("firstName") String fname, @FormParam("lastName") String lname,
                              @FormParam("salary") BigDecimal salary,
                              @Context UriInfo uriInfo) {
    init();
    Integer id = manageEmployee.addEmployee(fname, lname, salary);
    GenericEntity<Integer> genericid = new GenericEntity<Integer>(id) {};
    return Response.ok(genericid).type(MediaType.APPLICATION_XML).build();
}

并且尝试在客户端再次获得Integer:

@Override
public Integer addEmployee(String fname, String lname, BigDecimal salary) {
Form form = new Form();
form.param("firstName", fname);
form.param("lastName", lname);
form.param("salary", salary.toString());
Response response = client.target(REST_SERVICE_URL).request(MediaType.APPLICATION_XML)
    .put(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
Integer  genericid = response.readEntity(new GenericType<Integer>() {});

System.out.println("Test case name: test addEmployee, Result: " + genericid);
return genericid;
}

来自控制台的问题:

test add employee Web Service Method
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=utf-8, type=class java.lang.Integer, genericType=class java.lang.Integer.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:834)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:368)
    at org.glassfish.jersey.client.InboundJaxrsResponse$2.call(InboundJaxrsResponse.java:126)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
    at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:267)
    at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:123)
    at com.deepam.ExampleHibernateEmployeeRESTfulTester.addEmployee(ExampleHibernateEmployeeRESTfulTester.java:83)
    at com.deepam.ExampleHibernateEmployeeRESTfulTester.main(ExampleHibernateEmployeeRESTfulTester.java:154)

我很奇怪,该响应的格式为text / html(第一行 - media type = text / html)。 谁能解释我,有什么不对?

1 个答案:

答案 0 :(得分:0)

这个问题是我的误解。 Form类中的参数本身为String。比原生方式是再次在服务器端接收String以避免null,格式错误等问题。在完成任务后可以转换为适当的对象。这是第一件事。所以我决定在BigDecimal条款中提供String@FormParam("salary") BigDecimal salary。第二件事是IntegerGenericType/GenericEntity作为回应。再次。最简单的方法是发送String并将其包装在客户端的“整数”中,以避免出现500 - server internal errorclass Integer has no its Default constructor等错误类型。