休息Web服务获取对象作为响应

时间:2015-08-31 06:37:00

标签: java apache web-services rest 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

谁能告诉我我做错了什么?我希望得到一个$(document).ready(function () { $('.btn').click(function () { var showMessage = $(this).attr("value"); console.log(showMessage); if (showMessage != "Clear") { $('#showProcess').val(function (n, c) { return c + showMessage; }); } else if (showMessage == "Clear") { $('#showProcess').val(""); // Clear } }); $('.btn').hover( function () { $(this).addClass('active'); }, function () { $(this).removeClass('active'); }); }); 对象作为回复。

1 个答案:

答案 0 :(得分:0)

你能尝试像这样更新你的服务器类吗?您可能需要将扩展​​映射添加到服务器工厂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();