Skip to content

Commit

Permalink
finisehd all
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed May 24, 2023
1 parent 1efde6e commit df666b2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
11 changes: 10 additions & 1 deletion ddd/multi-layers/src/main/java/org/hzz/controller/TestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hzz.domain.common.Result;
import org.hzz.domain.dto.EmailQueryDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Tag(name = "测试")
@RequestMapping("/test")
Expand All @@ -22,4 +24,11 @@ default Result<String> exception() {
default Result<String> hello() {
return Result.success("hello Q10Viking");
}

@Operation(summary = "测试校验getter还是setter")
@GetMapping("/validate")
default Result<String> validate(@Valid EmailQueryDTO phoneQueryDTO) {
System.out.println("....");
return Result.success(phoneQueryDTO.getEmail());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -70,6 +71,25 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
return new ResponseEntity<>(objectBody, status);
}

@Override
protected ResponseEntity<Object> handleBindException(BindException exception, HttpHeaders headers, HttpStatus status, WebRequest request) {
logger.info("BindException");
Map<String, Object> objectBody = new LinkedHashMap<>();
objectBody.put("Current Timestamp", new Date());
objectBody.put("Status", status.value());

// Get all errors
List<String> exceptionalErrors = exception.getBindingResult()
.getFieldErrors()
.stream()
.map(x -> x.getField()+":"+x.getDefaultMessage())
.collect(Collectors.toList());

objectBody.put("Errors", exceptionalErrors);

return new ResponseEntity<>(objectBody, status);
}

/**---------------------以下是扩展的异常--------------------------------------*/
@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
public Result<String> handleMySelfException(Exception e, WebRequest request){
Expand All @@ -81,4 +101,6 @@ public Result<String> handleMySelfException(Exception e, WebRequest request){

return Result.error(e.toString(), ""+HttpStatus.INTERNAL_SERVER_ERROR.value(),msgBuilder.toString());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.hzz.domain.dto;

import javax.validation.constraints.Email;

//@Validated
public class EmailQueryDTO {
@Email(message = "邮箱格式不正确")
private String email;

public EmailQueryDTO() {
}

public @Email(message = "邮箱格式不正确") String getEmail() {
return this.email;
}

public void setEmail(@Email(message = "邮箱格式不正确") String email) {
this.email = email;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EmailQueryDTO)) return false;
final EmailQueryDTO other = (EmailQueryDTO) o;
if (!other.canEqual((Object) this)) return false;
final Object this$email = this.getEmail();
final Object other$email = other.getEmail();
if (this$email == null ? other$email != null : !this$email.equals(other$email)) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof EmailQueryDTO;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $email = this.getEmail();
result = result * PRIME + ($email == null ? 43 : $email.hashCode());
return result;
}

public String toString() {
return "EmailQueryDTO(email=" + this.getEmail() + ")";
}
}
44 changes: 40 additions & 4 deletions validation/hibernate-use/src/main/java/org/hzz/phone/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
package org.hzz.phone;

import lombok.Data;

@Data
public class User {

@MyPhone

private String phonenumber;

public User() {
}

public @MyPhone String getPhonenumber() {
return this.phonenumber;
}

public void setPhonenumber(@MyPhone String phonenumber) {
this.phonenumber = phonenumber;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof User)) return false;
final User other = (User) o;
if (!other.canEqual((Object) this)) return false;
final Object this$phonenumber = this.getPhonenumber();
final Object other$phonenumber = other.getPhonenumber();
if (this$phonenumber == null ? other$phonenumber != null : !this$phonenumber.equals(other$phonenumber))
return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof User;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $phonenumber = this.getPhonenumber();
result = result * PRIME + ($phonenumber == null ? 43 : $phonenumber.hashCode());
return result;
}

public String toString() {
return "User(phonenumber=" + this.getPhonenumber() + ")";
}
}

0 comments on commit df666b2

Please sign in to comment.