簡體   English   中英

REST Web服務獲取對象作為響應

[英]Rest web service getting an object as a response

我編寫了一個簡單的應用程序,使用Apache CXF創建了服務器和客戶端。 我希望當客戶端請求特定消息時,服務器將返回此消息。

我的密碼

public class Message {
    private long id;
    private String message;
    private String author;

    public Message() { }

    public Message(long id, String message, String author) {
        this.id = id;
        this.message = message;
        this.author = author;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}


@Path("/messages")
public class MessageResource {

    private MessageService messageService = new MessageService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Message> getMessages() {
        return messageService.getAllMessages();
    }

    @GET
    @Path("/{messageId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Message getMessage(@PathParam("messageId") int messageId) {
        return messageService.getMessage(messageId);
    }

}

public class MessageService {

    private Map<Integer, Message> messages = new HashMap<>();

    public MessageService() {
        messages.put(1, new Message(1, "msg1", "author1"));
        messages.put(2, new Message(2, "msg2", "author2"));
        messages.put(3, new Message(3, "msg3", "author3"));
        messages.put(4, new Message(4, "msg4", "author4"));
        messages.put(5, new Message(5, "msg5", "author5"));
    }

    public List<Message> getAllMessages() {
        return new ArrayList<Message>(messages.values());
    }

    public Message getMessage(int id) {
        return messages.get(id);
    }

}

public class Client 
{

    public static void main(String[] args) 
    {
        JAXRSClientFactoryBean client = new JAXRSClientFactoryBean();
        client.setResourceClass(Message.class);
        client.setAddress("http://localhost:8080/messenger/messages/4");
        WebClient wc = client.createWebClient();
        Message msg = wc.get(Message.class);
        System.out.println(msg);
    }
}

public class Server {

    public static void main(String[] args) {
        JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
        serverFactory.setResourceClasses(MessageResource.class);
        serverFactory.setResourceProvider(MessageResource.class, 
                new SingletonResourceProvider(new MessageResource()));
        serverFactory.setAddress("http://localhost:8080/messenger/");
        serverFactory.setProvider(JacksonJsonProvider.class);
        serverFactory.create();
    }

}

我收到此錯誤: No message body reader has been found for class app.Message, ContentType: application/json

誰能告訴我我在做什么錯? 我想獲得一個Message對象作為響應。

您可以嘗試像這樣更新服務器類嗎? 您可能需要將擴展​​映射添加到Server Factory Bean。

JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
serverFactory.setResourceClasses(MessageResource.class);
serverFactory.setResourceProvider(MessageResource.class, new SingletonResourceProvider(new MessageResource()));
serverFactory.setAddress("http://localhost:8080/messenger/");
Map<Object, Object> mappings = new HashMap<Object, Object>(); 
mappings.put("xml", "application/xml"); 
mappings.put("json", "application/json"); 
serverFactory.setExtensionMappings(mappings); 
serverFactory.setProvider(JacksonJsonProvider.class);
serverFactory.create();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM