2017-08-30 31 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

謝謝,但仍然無法正常工作。 –