thắc mắc [Java] Thắc mắc về Java

xem import có đúng cách ko. Nhìn structure như kiểu ko phải maven hay gradle project. thì phải tự import thư viện vào trong project structure.
dạ có keyword nào để em tìm hiểu tự làm ko ạ, em chưa rõ phải nên làm gì ạ. bth có maven nó tự tải thư viện về mà project này ko có file pom. Thư viện là tự import trong file lib.
 
các bác cho em hỏi xíu là 1 project được tạo từ eclip có thể mở và chạy ở intelliJ không ạ, Nay cầm project của cty mà cty dùng eclip, em mở intelliJ thì lại không được. Do cty ko nhất thiết dùng IDE nào nên muốn code bằng thằng intelliJ :(
được nhé, lúc "new project from existing source" thì nó tự động detect eclipse project rồi import nhé
 
Mọi người cho em hỏi là có cách nào sử dụng postman để có thể đưa được cả json và file (form-data) vào một request không ạ. Cụ thể là một user em muốn khi update thông tin thì có thể nhấn một phát các info sẽ thay đổi (trong đó có cả avatar ạ)
 
Mọi người cho em hỏi là có cách nào sử dụng postman để có thể đưa được cả json và file (form-data) vào một request không ạ. Cụ thể là một user em muốn khi update thông tin thì có thể nhấn một phát các info sẽ thay đổi (trong đó có cả avatar ạ)
Đơn giản là bạn có thể để client biến file (ảnh avatar thành base64) rồi post json bình thường thôi.
 
Đơn giản là bạn có thể để client biến file (ảnh avatar thành base64) rồi post json bình thường thôi.
Nếu vậy thì nó phải encode từ trên client chứ ạ. Mà em thấy việc đó để ở trên client nó cứ thế nào ấy
 
Mọi người cho em hỏi nguyên lý hoạt động của cái custom validation này với ạ. Em có làm theo trên Stackoverflow cơ mà áp vào project thì nó vẫn không validate được ạ. Kiểu như nếu em gửi request là một file .txt thì nó vẫn nhận và lưu vào chứ không bị chặn lại ấy ạ

Java:
[ICODE]
@PostMapping("/profile/avatar")

@Validated

public ResponseEntity<String> updateAvatar(

        @RequestPart ("image") @Valid @ValidImage MultipartFile multipartFile) throws IOException {

...
[/ICODE]

Java:
[ICODE]
@Target({ ElementType.PARAMETER, ElementType.FIELD })

@Retention(RetentionPolicy.RUNTIME)

@Constraint(validatedBy = {ImageFileValidator.class})

public @interface ValidImage {

    String message() default "Invalid image file";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
[/ICODE]

Java:
[ICODE]
public class ImageFileValidator implements ConstraintValidator<ValidImage, MultipartFile> {

    @Override
    public void initialize(ValidImage constraintAnnotation) {
    }

    @Override
    public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {

        String contentType = multipartFile.getContentType();

        if (!isSupportedContentType(contentType)) {
            context.disableDefaultConstraintViolation();

            context.buildConstraintViolationWithTemplate("Only PNG or JPG images are allowed")

                    .addConstraintViolation();
        }

        return true;
    }

    private boolean isSupportedContentType(String contentType) {

        return contentType.equals("image/png")

                || contentType.equals("image/jpg")

                || contentType.equals("image/jpeg");

    }

}
[/ICODE]
 
Java:
[ICODE]
public class ImageFileValidator implements ConstraintValidator<ValidImage, MultipartFile> {

    @Override
    public void initialize(ValidImage constraintAnnotation) {
    }

    @Override
    public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {

        String contentType = multipartFile.getContentType();

        if (!isSupportedContentType(contentType)) {
            context.disableDefaultConstraintViolation();

            context.buildConstraintViolationWithTemplate("Only PNG or JPG images are allowed")

                    .addConstraintViolation();
        }

        [B]return true;[/B]
    }

    private boolean isSupportedContentType(String contentType) {

        return contentType.equals("image/png")

                || contentType.equals("image/jpg")

                || contentType.equals("image/jpeg");

    }

}
[/ICODE]
cái này nó luôn return true mà. Cái hàm Isvalid phải return false nó mới chặn
 
cái này nó luôn return true mà. Cái hàm Isvalid phải return false nó mới chặn
À em có sửa mà nói vẫn bị bỏ qua bác ấy ạ


Java:
    @Override
    public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {

        boolean result = true;

        String contentType = multipartFile.getContentType();
        if (!isSupportedContentType(contentType)) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate("Only PNG or JPG images are allowed")
                    .addConstraintViolation();

            result = false;
        }

        return result;
    }
 
Có bác nào từng làm về các sàn giao dịch ko nhỉ, em nghe nói phần lớn các sàn đều dùng thư viện Lmax Exchange của Java, mà cái này google ít tài liệu quá
 
Mọi người cho em hỏi nguyên lý hoạt động của cái custom validation này với ạ. Em có làm theo trên Stackoverflow cơ mà áp vào project thì nó vẫn không validate được ạ. Kiểu như nếu em gửi request là một file .txt thì nó vẫn nhận và lưu vào chứ không bị chặn lại ấy ạ

Java:
[ICODE]
@PostMapping("/profile/avatar")

[B]@Validated[/B]

public ResponseEntity<String> updateAvatar(

        @RequestPart ("image") @Valid @ValidImage MultipartFile multipartFile) throws IOException {

...
[/ICODE]
Java:
@Validated
move annotation này ra ngoài class level, là đc :D
1656237105582.png

bonus, simplify cái này lại nv thôi :D

Java:
[ICODE]
@Override
public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
    List<String> validTypes = Arrays.asList("image/png", "image/jpg", "txt");
    boolean rs = validTypes.contains(value.getContentType());
    if (!rs) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate("Only PNG or JPG images are allowed")
                .addConstraintViolation();
    }
    return rs;
}
[/ICODE]
 

Attachments

  • 1656237096398.png
    1656237096398.png
    32.8 KB · Views: 43
Last edited:
Java:
@Validated
move annotation này ra ngoài class level, là đc :D
View attachment 1231850
bonus, simplify cái này lại nv thôi :D

Java:
[ICODE]
@Override
public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
    List<String> validTypes = Arrays.asList("image/png", "image/jpg", "txt");
    boolean rs = validTypes.contains(value.getContentType());
    if (!rs) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate("Only PNG or JPG images are allowed")
                .addConstraintViolation();
    }
    return rs;
}
[/ICODE]
À ok rồi bác ạ, em cảm ơn nhiều :p
 
Java nhắm ôm vào CTY xịn luôn thì ôm, ko thì học cái nào nhiều việc ấy, điểm chung của mấy cái framework này đều là DI framework của mấy ngôn ngữ class based, à có thằng giả cầy nestjs là prototype nhưng viết bằng typescript như java spring :baffle:, nắm đc 1 cái thì mấy cái còn lại cũng nhanh thôi

Gửi từ OPPO CPH1989 bằng vozFApp
em đang lưỡng lự nên phát triển lâu dài với spring hay nestjs, em mới đầu năm 3 đại học thôi, năm sau thực tập làm nestjs kbiet có chỗ nào nhận không
 
em đang lưỡng lự nên phát triển lâu dài với spring hay nestjs, em mới đầu năm 3 đại học thôi, năm sau thực tập làm nestjs kbiet có chỗ nào nhận không

Lật spring ra làm thử cái web bán hàng sau đó lật nest ra làm thử cái web bán hàng luôn, cuối cùng thích cái nào hơn thì pick, vẫn ko lựa đc thì CV cũng có 2 cái pet project lúc đó xin việc đc rồi :rolleyes:
 
Lật spring ra làm thử cái web bán hàng sau đó lật nest ra làm thử cái web bán hàng luôn, cuối cùng thích cái nào hơn thì pick, vẫn ko lựa đc thì CV cũng có 2 cái pet project lúc đó xin việc đc rồi :rolleyes:
thực tế là thì em thích Nestjs (Nodejs thì đúng hơn) hơn vì ecosystem nó to :LOL:))) nhưng viết Java thì vui hơn, đang chuẩn bị có 1 quả personal project cũng to, em định làm bằng spring nhưng nestjs cũng hay nên lưỡng lự :LOL:))
 
thực tế là thì em thích Nestjs (Nodejs thì đúng hơn) hơn vì ecosystem nó to :LOL:))) nhưng viết Java thì vui hơn, đang chuẩn bị có 1 quả personal project cũng to, em định làm bằng spring nhưng nestjs cũng hay nên lưỡng lự :LOL:))

Ông bị ngược à, NodeJS nào eco to, java mới đúng là eco nó to nhé :rolleyes:
 
Back
Top