我们有一个 Java 应用程序,它通过 IBM WebSphere MQ 发送 JMS 消息。消费者应用程序要求将消息内容类型设置为“application/json”。我该怎么做?
我已经检查了一些参考资料,似乎我可以通过方法“setStringProperty(headerKey, headerName)”设置额外的标题,例如
Message jmsMsg = session.createTextMessage(msgStr);
jmsMsg.setStringProperty("Content-Type", "application/json");
Run Code Online (Sandbox Code Playgroud)
问题是“Content-Type”不是有效的属性键,因为它包含“-”字符。
这是可以在代码中完成的事情吗?或者它实际上是在队列设置中配置的?
属性名称“Content-Type”具有“-”字符。根据 JMS 规范,属性名称可以包含 JavaCharacter.isJavaIdentifierPart方法为其返回true. 对于'-'字符isJavaIdentifierPart方法返回false。因此,setStringProperty("Content-Type", "application/json")方法调用失败并出现以下异常。
com.ibm.msg.client.jms.DetailedMessageFormatException: JMSCC0049: The property name 'Content-Type' is not a valid Java(tm) identifier.
The supplied property name does not conform to the allowed format described in the JMS specification.
Check the characters used in the property name and modify as necessary.
Run Code Online (Sandbox Code Playgroud)
如果可以更改接收应用程序,您可以选择"Content_Type"(使用下划线)作为属性名称而不是"Content-Type".
| 归档时间: |
|
| 查看次数: |
5924 次 |
| 最近记录: |