休息网络服务获取对象作为响应
Rest web service getting an object as a response
我写了一个简单的应用程序,使用 Apache CXF
创建了一个服务器和一个客户端。我希望当客户端请求某个消息时,服务器将 return 这条消息。
我的代码
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
对象作为响应。
你能尝试像这样更新你的服务器 class 吗?您可能需要将扩展映射添加到服务器工厂 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();
我写了一个简单的应用程序,使用 Apache CXF
创建了一个服务器和一个客户端。我希望当客户端请求某个消息时,服务器将 return 这条消息。
我的代码
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
对象作为响应。
你能尝试像这样更新你的服务器 class 吗?您可能需要将扩展映射添加到服务器工厂 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();