2017-06-15 105 views
1

即使我在maven中添加了媒體moxy,我得到了org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException。我正在使用tomcat 8.5,我仍然不知道爲什麼我得到這個異常。我正在嘗試使用@valid註釋來驗證實體。org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒體的MessageBodyWriter

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList. 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1118) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:662) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:418) 
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:461) 
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:311) 
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) 
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) 
    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:267) 
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) 
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286) 
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072) 
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399) 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source)`package com.seriesonline.app.seriestv.ws.exception.mappers; 

這是我的異常映射

import java.util.ArrayList; 
import java.util.List; 

import javax.validation.ConstraintViolation; 
import javax.validation.ConstraintViolationException; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.ext.ExceptionMapper; 
import javax.ws.rs.ext.Provider; 

import com.seriesonline.app.seriestv.models.Errors; 

@Provider 
public class BeanValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> { 

    @Override 
    public Response toResponse(ConstraintViolationException ex) { 
     System.out.println("*** BeanValidationExceptionMapper ***"); 
     List<Errors> errors = new ArrayList<>(); 

     for (ConstraintViolation<?> cv : ex.getConstraintViolations()) { 
      Errors error = new Errors(); 
      error.setDescription("Description"); 
      error.setErrorMessage(cv.getMessage()); 
      error.setStatusCode(400); 
      errors.add(error); 
     } 

     return Response.status(400).entity(errors).type(MediaType.APPLICATION_JSON).build();// Response.status(400).entity(errors).type(MediaType.APPLICATION_JSON).build(); 
    } 

} 

Maven的

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy --> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-moxy</artifactId> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-jaxb --> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-jaxb</artifactId> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart --> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-multipart</artifactId> 
     </dependency> 

感謝大家的時間......我希望我能解決這個問題。

+0

固定的,這似乎是一種錯誤或類似的東西嗎?它有點像是如果它不能解析一個ArrayList到JSON,嗯,如果它是錯誤對象,只是一個對象,它工作正常,但問題出現時,它是一個對象的數組列表,它看起來像實體(對象)方法無法將實體解析爲JSON,所以我只是... Gson gson = new Gson(); \t \t \t \t String JSON = gson.toJson(errors); \t \t \t \t System.out.println(JSON); \t \t return Response.status(400).entity(JSON).type(MediaType.APPLICATION_JSON).build(); –

回答

0

隨着媒體MOXY ...

GenericEntity <List<MyClass>> g = new GenericEntity <List<MyClass>>(myList){};... return Response.status(400).entity(g).type(MediaType.APPLICATION‌​_‌​JSON).build(); 
相關問題