`readEntity(Object.class)` 无法从实体流中反序列化对象。

huangapple go评论106阅读模式
英文:

readEntity(Object.class) cannot deserializing object from entity stream

问题

我必须从数据库中获取用户。我正在使用String Boot的微服务。这是代码:

@RequestMapping(value = "/obtener/{nombre}", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> ObtenerUsuario(@PathVariable(value = "nombre", required = true) String nombre) {
    System.out.println("Acceso al microservicio: http://localhost:11708/obtener");
    List<Usuario> usuarios = usuariosDAO.findByNombre(nombre);
    
    if (usuarios.size() > 1) {
        System.out.println("ERROR: Varios usuarios tienen el mismo nombre");
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
    if (!usuarios.isEmpty()) {
        System.out.print("El nombre de usuario se ha encontrado");
        return new ResponseEntity<>(usuarios.get(0), HttpStatus.FOUND);
    }
    
    System.out.println("El usuario no se ha encontrado");
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

我从Servlet中这样调用它:

Client client = ClientBuilder.newClient();
WebTarget webResource = client.target("http://localhost:11708/obtener").path(nombre);
System.out.println("Llamada al microservicio: " + webResource.toString());
Invocation.Builder invocationBuilder = webResource.request(new String[] { "application/json" });
Response respuesta = invocationBuilder.get();

switch (respuesta.getStatus()) {
    ...
    case 302: {
        System.out.println("Nombre de usuario correcto");
        Usuario usuario = respuesta.readEntity(Usuario.class);
    }
    ...
}

但是readEntity(Usuario.class) 无法从实体流中反序列化对象,抛出以下错误:

StandardWrapperValve[controlador.ServletController]: Servlet.service() for servlet controlador.ServletController threw exception
javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
...

你知道我犯了什么错误吗?谢谢。

英文:

I have to get user from the data base. I'm using a microservice from String boot. This is the code:

@RequestMapping(value = &quot;/obtener/{nombre}&quot;, method = RequestMethod.GET, produces = &quot;application/json&quot;)
public ResponseEntity&lt;?&gt; ObtenerUsuario(@PathVariable(value = &quot;nombre&quot;, required = true) String nombre) {
		
	System.out.println(&quot;Acceso al microservicio: http://localhost:11708/obtener&quot;);
		
	List&lt;Usuario&gt; usuarios = usuariosDAO.findByNombre(nombre);
		
	if(usuarios.size()&gt;1) {
		System.out.println(&quot;ERROR: Varios usuarios tienen el mismo nombre&quot;);
		return new ResponseEntity&lt;&gt;(HttpStatus.INTERNAL_SERVER_ERROR);
	}
		
	if(!usuarios.isEmpty()) {
		System.out.print(&quot;El nombre de usuario se ha encontrado&quot;);
		return new ResponseEntity&lt;&gt;(usuarios.get(0), HttpStatus.FOUND);
	}
		
	System.out.println(&quot;El usuario no se ha encontrado&quot;);
	return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
}

I call it from the Servlet using this:

    Client client = ClientBuilder.newClient();
    WebTarget webResource = client.target(&quot;http://localhost:11708/obtener&quot;).path(nombre);
    System.out.println(&quot;Llamada al microservicio: &quot; + webResource.toString());
    Invocation.Builder invocationBuilder = webResource.request(new String[] { &quot;application/json&quot; });
    Response respuesta = invocationBuilder.get();
    
    switch (respuesta.getStatus()) {
        ...
        case 302: {
            System.out.println(&quot;Nombre de usuario correcto&quot;);
            Usuario usuario = respuesta.readEntity(Usuario.class);
        }
        ...
    }

But readEntity(Usuario.class) cannot deserializing object from entity stream and it throw this:

StandardWrapperValve[controlador.ServletController]: Servlet.service() for servlet controlador.ServletController threw exception
javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:101)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:321)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
...

Do you know where did I mistake? Thank you.

答案1

得分: 0

我终于解决了。我的问题是我没有为这些Java对象创建空构造函数。JSON会使用这些函数作为默认值。

英文:

I finally resolve it. My problem was I didn't create empty constructors for the java objects. JSON use those for default.

huangapple
  • 本文由 发表于 2020年10月27日 21:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64555277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定