2017-08-30 37 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サービス、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

ありがとうございますが、まだ動作しません。 –