Menu

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, June 28, 2021

[Java - Spring Boot] Exception Handler ใน Spring Boot
[Java - Spring Boot] Exception Handler in Spring Boot

สำหรับ Exception Handler เราจะสร้าง class ที่ใช้ @ControllerAdvice ดังนี้
package com.example.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.LinkedHashMap;

@ControllerAdvice
public class CustomExceptionHandler   {

    @ExceptionHandler(value = {ExceptionWrapper.class})(value = {exception_class.class})
    public ResponseEntity<Object> exceptionHandler(exception_class ex) {
    	// Handle specific exception then return ResponseEntity
        return new ResponseEntity(ex,HttpStatus.BAD_REQUEST);
    }
}
      
โดยที่ exception_class เป็นชื่อของ Exception Class ที่ต้อง handle 

คำอธิบายเพิ่มเติม
  • @ControllerAdvice - อนุญาตให้เรา apply เทคนิคใน class ข้างใต้มันกับทุกๆ controller ของเรา (อ่านเพิ่มเติมที่ ControllerAdvice)
  • @ExceptionHandler - ใช้เพื่อระบุว่า method ข้างใต้มันเป็น Exception handling methods (อ่านเพิ่มเติมที่ ExceptionHandler)

[Java - Spring Boot] วิธีการสร้าง JSON response สำหรับทุก controller ใน Spring Boot
[Java - Spring Boot] How to Wrap JSON Response for All Controllers in Spring Boot

หลังจากเรากำหนด @RestController ที่เหนือ class ของ controller ทำให้ object ที่ return จาก controller นั้นจะถูกแปลงไปเป็น JSON response ให้โดยอัตโนมัติ

อย่างไรก็ตาม ถ้าหากเราต้องการสร้าง template ครอบ object ที่ return จาก controller ทุกๆ controller ก่อนที่จะส่งเป็น JSON response ออกไป เรามีขั้นตอนดังนี้

  1. สร้าง Class สำหรับ JSON response template
    package com.example.model.wrapper;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import org.springframework.http.HttpStatus;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class ResultWrapper<T> {
        @JsonProperty("code")
        private Integer code;
        @JsonProperty("status")
        private HttpStatus status;
        @JsonProperty("message")
        private String message;
        @JsonProperty("data")
        private T data = null;
    
    
        public Result(Integer code, HttpStatus status, String message) {
            this.code = code;
            this.status = status;
            this.message = message;
        }
    
        public ResultWrapper(Integer code, HttpStatus status, T data) {
            this.code = code;
            this.status = status;
            this.data = data;
        }
    }
    

Thursday, August 6, 2020

[Android - Espresso] การติดตั้ง Java เพื่อรัน uiautomatorviewer
[Android - Espresso] How to Install Java to Run uiautomatorviewer

uiautomatorviewer ใช้ในการหารายละเอียดของ element ที่ปรากฎบนหน้าจอ

โดยปกติแล้ว uiautomatorviewer จะอยู่ที่ {android_sdk_path}\tools\bin 
โดยที่ {android_sdk_path} บน Windows จะอยู่ที่ C:\Users\{username}\AppData\Local\Android\Sdk

ถ้าหากรัน uiautomatorviewer แล้วได้ error ตามด้านล่างนี้
-Djava.ext.dirs=/Users/<Username>/Library/Android/sdk/tools/lib/x86_64:/Users/<Username>/Library/Android/sdk/tools/lib is not supported.  Use -classpath instead.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
สาเหตุที่ทำให้เกิด error เนื่องจาก version ของ Java ที่ติดตั้งอยู่ในเครื่องไม่เข้ากับ version ที่ uiautomatorviewer ใช้รัน

วิธีแก้ error คือ เปลี่ยนไป Java version 8 โดยดาวน์โหลดได้จาก https://www.java.com/en/download/

หมายเหตุ ถ้าดาวน์โหลด Java จาก https://www.oracle.com/java/technologies/javase-downloads.html จะเกิด error ข้างต้น