【发布时间】:2020-09-26 19:13:59
【问题描述】:
我正在尝试构建一个简单的 JSON-B 程序,它将 java 对象转换为 JSON 字符串。该程序还使用 pojo 上的 @JsonbProperty 注解,将不匹配的 json 属性映射到 java 对象。
然后将字符串发送到 JMS 队列,供 JMS 使用者提取。我正在使用 bash 测试脚本,在控制台中打印序列化的 json 输出。已经提供了测试脚本和 .json 文件,我只是假设它们是正确的。
当我运行 bash 脚本时,我收到“404 Not Found”错误,控制台中没有输出。我附上了 java 代码、.json 文件、测试脚本和错误消息。
任何帮助将不胜感激。
提前谢谢..
Bash 脚本
curl -X POST -H "Content-Type: application/json" -d @order.json http://localhost:8080/hsports-catalog-jax/hsports/api/order
Bash 脚本终端错误
[21:27] ~/IdeaProjects/JEE8_Essential_Training/HSports_CatalogProject/jaxrs_module/src/resources Marc's Mac >> ./test.sh
<html><head><title>Error</title></head><body>404 - Not Found</body></html>[21:27] ~/IdeaProjects/JEE8_Essential_Training/HSports_CatalogProject/jaxrs_module/src/resources Marc's Mac >>
JAX-RS 端点
@RequestScoped
@Path("/order")
@Produces("application/json")
@Consumes("application/json")
public class OrderEndpoint {
// injects JMS producer from ejb module
@Inject
private JmsService jmsService;
// method for placing an order
// accepts Order object JAX-RS resource
// method will convert it to json
@POST
public void placeOrder(Order order) {
Jsonb jsonb = JsonbBuilder.create(); // json builder object
String json = jsonb.toJson(order); // converts order object to json representation
System.out.println(json);
jmsService.send(json);
}
}
JMS 生产者
@ApplicationScoped
public class JmsService {
// injects JMS queue we want to send the message to
@Resource(mappedName = "java:/jms/queue/HsportsQueue") // JNDI name
private Queue hsportsQueue;
@Inject
@JMSConnectionFactory("java:/ConnectionFactory")
private JMSContext context;
// code that sends the message to the consumer
public void send(String message) {
try {
TextMessage textMessage = context.createTextMessage(message); // message object
context.createProducer().send(hsportsQueue, textMessage); // producer object
System.out.println("Message sent to JMS queue"); // console println
} catch (Exception e) {
e.printStackTrace();
}
}
}
JMS 消费者
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(
propertyName = "destination", propertyValue = "/jms/queue/HsportsQueue"),
@ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue")
},
mappedName = "/jms/queue/HsportsQueue")
public class JmsConsumerBean implements javax.jms.MessageListener {
public JmsConsumerBean() {
}
// defines what the consumer does, when msg is received from jms queue
@Override
public void onMessage(Message message) {
System.out.println("Message received, JMS Consumer message-driven bean");
try {
System.out.println(message.getBody(String.class));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Java Pojo
public class Order {
private Long orderId;
private String storeName;
private Customer customer; // new class created
private List<InventoryItem> items;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public List<InventoryItem> getItems() {
return items;
}
// maps json-inventoryItems to java-item
@JsonbProperty("inventoryItems")
public void setItems(List<InventoryItem> items) {
this.items = items;
}
}
JSON 文件内容
{
"orderId": 1,
"storeName": "Franklin Park",
"customer": {
"customerId": 1,
"firstName": "Kevin",
"lastName": "Bowersox"
},
"inventoryItems": [
{
"inventoryItemId": 1,
"catalogItemId": 1,
"name": "Sneakers",
"quantity": 4
}
]
}
【问题讨论】:
标签: bash jakarta-ee jax-rs jms jsonb