简体   繁体   中英

Utf-8 String in Spring Boot service

I'm making a service to send notify to my web application through Firebase, here is my code:

  @Override
public String sendPushNotification() throws IOException {
    String result = "";

    URL url = new URL(Constants.FIREBASE_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + Constants.FIREBASE_SERVER_KEY);
    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3)" +
            " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");


    JSONObject json = new JSONObject();

    Notification notification = notificationRepository.getFirstByOrderByIdDesc();
    AssignTask assignTask = assignTaskService.getAssignTask(notification.getTaskId());

    try {

        json.put("to", accountService.findByEmployeeId(assignTask.getAssignerId()).getTokenFirebase().trim());


        JSONObject data = new JSONObject();
        data.put("sender", employeeService.getDetailEmployee(assignTask.getAssigneeId()).getFullName());
        data.put("task_id", notification.getTaskId());
        json.put("data", data);
        JSONObject info = new JSONObject();
        info.put("title", notification.getLabel());
        info.put("body", notification.getContent());
        System.out.println(notification.getContent());
        info.put("message", "");
        json.put("notification", info);

    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    try {

        System.out.println(json.toString());
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        result = "succcess";
    } catch (Exception e) {
        e.printStackTrace();
        result = "failure";
    }
    System.out.println("FCM Notification is sent successfully");

    return result;
}

When I debug, here is the json value

{"notification":{"title":"Báo cáo công việc","body":"đã gửi báo cáo về công việc có ID = ","message":""},"data":{"sender":"Đen Vâu","task_id":35},"to":"cAwBXnEql-A:APA91bF2R1iIugxiIT9iR8NtNGoeOGXLHAdCbf07ukJ0Rjp6MjJb3nTrP9X1W6ZBj-MWWa6WnpVRVC3EXqPdiGi--kN5tmFySgo8IlN9Vq2_-DxNx7T4jiYgeXf9hDdBpkrboNjWtGe4"}

But the message send to Firebase is in wrong format, I put an System.out.println() and it showed this:

{"notification":{"title":"Báo cáo công vi?c","body":"?ã g?i báo cáo v? công vi?c có ID = ","message":""},"data":{"sender":"?en Vâu","task_id":35},"to":"cAwBXnEql-A:APA91bF2R1iIugxiIT9iR8NtNGoeOGXLHAdCbf07ukJ0Rjp6MjJb3nTrP9X1W6ZBj-MWWa6WnpVRVC3EXqPdiGi--kN5tmFySgo8IlN9Vq2_-DxNx7T4jiYgeXf9hDdBpkrboNjWtGe4"}

I tried to many ways but it still no help. Anyone know why? I think this is an issue with UTF-8 of Spring Boot. I'm using Spring Boot and Gradle

This is late but may help somebody resolve this issue.

Run spring boot jar with this command:

java -Dfile.encoding=UTF-8 -jar hello.jar

You can get more info about -Dfile.encoding=UTF-8 from this Setting the default Java character encoding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM