2017-08-30 36 views
-1

我正在学习如何处理Java中的其余服务。我需要使用JSON,但我得到一个convertToException。 (我正在使用org.glassfish.jersey.containers,com.sun.net.httpserver和javax.ws.rs) org.glassfish.jersey.client.JerseyInvocation.convertToException在“response.get(Paper.class )”。我怎样才能将文件从JSON中取出?Java,rest service,JSON convertToException

这里是我的代码:

public class Start 
{ 
    static URI uri = URI.create("http://localhost:8080/rest"); 

    public static void main(String[] args) 
throws ProcessingException, URISyntaxException 
{ 
    ResourceConfig rc = new ResourceConfig(MessageResource.class); 
    HttpServer server = JdkHttpServerFactory.createHttpServer(uri, rc); 

    ClientConfig config = new ClientConfig(); 
    Client client = ClientBuilder.newClient(config); 
    WebTarget target = client.target(uri); 
    Builder response = target.path("message").path("paper").request() 
    .accept(MediaType.APPLICATION_JSON); 
    JOptionPane.showMessageDialog(null, 
     response.get(Paper.class).getAuthor() + "\n" 
     +response.get(Paper.class).getYear()+"\n" 
     +response.get(Paper.class).getTitel()+"\n" 
     +response.get(Paper.class).getJournal()); 
    server.stop(0); 
} 
} 

和:

@Path("message") 
public class MessageResource 
{ 
@GET 
@Path ("paper") 
@Produces(MediaType.APPLICATION_JSON) 
public Paper paper() 
{ 
    Paper paper = new Paper(); 
    paper.setAuthor("Hilde Heidefrau"); 
    paper.setJournal("Archaeology Journal"); 
    paper.setTitel("On the Venus of Willendorf."); 
    paper.setYear("2017"); 

    return paper; 
} 
} 

和纸张类:

public class Paper 
{ 
    private String author; 
private String year; 
private String titel; 
private String journal; 
// getters and setters here 
} 

回答

0

纸张类必须实现java.io.Serializable才能够被序列化。

+0

谢谢,但仍然无法正常工作。 –