无法在 post 请求中发送阿拉伯文本
cannot send Arabic text in post request
我仍然是 spring 引导的初学者,我遇到了 resTemplet
的问题,我使用 JPA 从数据库中获取数据,post 通过 HTTP post 请求,当 messageBody
是英语时一切正常,但对于阿拉伯语,消息看起来不可读,问题仅与阿拉伯语消息有关,我已经阅读了很多关于这件事的内容,并且我遵循了一些建议,例如:
1- 设置 application.properties
如:
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
server.tomcat.uri-encoding=UTF-8
2- 我试过 StringHttpMessageConverter
但没有添加任何值
restMessengerTemplate = new RestTemplate();
restMessengerHeader = new HttpHeaders();
restMessengerHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
restMessengerHeader.add("Authorization",
"Basic " + Base64.getEncoder().encodeToString(("user:password").getBytes()));
但问题仍然存在我也试过了headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
但是它returns字段MediaType.APPLICATION_JSON_UTF8已弃用
代码:
public class Messenger {
private static Messenger instance = new Messenger();
private static final RestTemplate restMessengerTemplate;
private static final HttpHeaders restMessengerHeader;
private HttpEntity<VasGateway> smsEntity;
private ResponseEntity<String> messengerResponseEntity;
static {
restMessengerTemplate = new RestTemplate();
restMessengerTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restMessengerHeader = new HttpHeaders();
restMessengerHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
restMessengerHeader.add("Authorization",
"Basic " + Base64.getEncoder().encodeToString(("user:password").getBytes()));
}
private Messenger() {
}
public static Messenger getInstance() {
return instance;
}
public String call(String messageId, String msisdn, String messageBody, String language) throws Exception {
smsEntity = new HttpEntity<VasGateway>(
new VasGateway(new Sendsms(new ArrayList<Message>(
Collections.singletonList(new Message(messageId, "Test", msisdn,
messageBody, language.equalsIgnoreCase("EN") ? "0" : "2"))))),restMessengerHeader);
messengerResponseEntity = restMessengerTemplate.exchange("http link",
HttpMethod.POST, smsEntity, String.class);
return "0";
}
}
我在 messageBody
的阿拉伯语文本中遇到的问题,我使用 oracle 11g 编码类型:AR8ISO8859P6
:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "messages" })
public class Sendsms {
@JsonProperty("messages")
private List<Message> messages = null;
public Sendsms() {
}
public Sendsms(List<Message> messages) {
this.messages = messages;
}
留言class:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "msgid", "from", "to", "text", "coding" })
public class Message {
@JsonProperty("msgid")
private String msgid;
@JsonProperty("from")
private String from;
@JsonProperty("to")
private String to;
@JsonProperty("text")
private String text;
@JsonProperty("coding")
private String coding;
public Message() {
}
public Message(String msgid, String from, String to, String text, String coding) {
this.msgid = msgid;
this.from = from;
this.to = to;
this.text = text;
this.coding = coding;
}
vasGetwayclass :
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "sendsms" })
public class VasGateway {
@JsonProperty("sendsms")
private Sendsms sendsms;
public VasGateway() {
}
public VasGateway(Sendsms sendsms) {
this.sendsms = sendsms;
}
my concern in arabic text at messageBody, im using oracle 11g with encoding type:AR8ISO8859P6 :
您在数据库中使用 ISO 8859-6(即 Latin/Arabic)编码,但您的代码需要 UTF-8。在某些时候,您将需要在两者之间进行转换。理想情况下,您应该将数据库直接转换为 UTF-8。但如果你不能这样做,你将不得不在数据库和 UTF-8 之间移动时进行转换 JSON.
我仍然是 spring 引导的初学者,我遇到了 resTemplet
的问题,我使用 JPA 从数据库中获取数据,post 通过 HTTP post 请求,当 messageBody
是英语时一切正常,但对于阿拉伯语,消息看起来不可读,问题仅与阿拉伯语消息有关,我已经阅读了很多关于这件事的内容,并且我遵循了一些建议,例如:
1- 设置 application.properties
如:
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
server.tomcat.uri-encoding=UTF-8
2- 我试过 StringHttpMessageConverter
但没有添加任何值
restMessengerTemplate = new RestTemplate();
restMessengerHeader = new HttpHeaders();
restMessengerHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
restMessengerHeader.add("Authorization",
"Basic " + Base64.getEncoder().encodeToString(("user:password").getBytes()));
但问题仍然存在我也试过了headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
但是它returns字段MediaType.APPLICATION_JSON_UTF8已弃用
代码:
public class Messenger {
private static Messenger instance = new Messenger();
private static final RestTemplate restMessengerTemplate;
private static final HttpHeaders restMessengerHeader;
private HttpEntity<VasGateway> smsEntity;
private ResponseEntity<String> messengerResponseEntity;
static {
restMessengerTemplate = new RestTemplate();
restMessengerTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restMessengerHeader = new HttpHeaders();
restMessengerHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
restMessengerHeader.add("Authorization",
"Basic " + Base64.getEncoder().encodeToString(("user:password").getBytes()));
}
private Messenger() {
}
public static Messenger getInstance() {
return instance;
}
public String call(String messageId, String msisdn, String messageBody, String language) throws Exception {
smsEntity = new HttpEntity<VasGateway>(
new VasGateway(new Sendsms(new ArrayList<Message>(
Collections.singletonList(new Message(messageId, "Test", msisdn,
messageBody, language.equalsIgnoreCase("EN") ? "0" : "2"))))),restMessengerHeader);
messengerResponseEntity = restMessengerTemplate.exchange("http link",
HttpMethod.POST, smsEntity, String.class);
return "0";
}
}
我在 messageBody
的阿拉伯语文本中遇到的问题,我使用 oracle 11g 编码类型:AR8ISO8859P6
:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "messages" })
public class Sendsms {
@JsonProperty("messages")
private List<Message> messages = null;
public Sendsms() {
}
public Sendsms(List<Message> messages) {
this.messages = messages;
}
留言class:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "msgid", "from", "to", "text", "coding" })
public class Message {
@JsonProperty("msgid")
private String msgid;
@JsonProperty("from")
private String from;
@JsonProperty("to")
private String to;
@JsonProperty("text")
private String text;
@JsonProperty("coding")
private String coding;
public Message() {
}
public Message(String msgid, String from, String to, String text, String coding) {
this.msgid = msgid;
this.from = from;
this.to = to;
this.text = text;
this.coding = coding;
}
vasGetwayclass :
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "sendsms" })
public class VasGateway {
@JsonProperty("sendsms")
private Sendsms sendsms;
public VasGateway() {
}
public VasGateway(Sendsms sendsms) {
this.sendsms = sendsms;
}
my concern in arabic text at messageBody, im using oracle 11g with encoding type:AR8ISO8859P6 :
您在数据库中使用 ISO 8859-6(即 Latin/Arabic)编码,但您的代码需要 UTF-8。在某些时候,您将需要在两者之间进行转换。理想情况下,您应该将数据库直接转换为 UTF-8。但如果你不能这样做,你将不得不在数据库和 UTF-8 之间移动时进行转换 JSON.