Java 如何查看从kotlin上的rest api返回的验证错误?

Java 如何查看从kotlin上的rest api返回的验证错误?,java,android,spring,kotlin,retrofit,Java,Android,Spring,Kotlin,Retrofit,我有一个基于spring的RESTAPI。正在检查我的dto类是否有错误 ErrorDetails.java public class ErrorDetails { public ErrorDetails(Date timestamp, String message, String details) { super(); this.timestamp = timestamp; this.message = message; this.details = detai

我有一个基于spring的RESTAPI。正在检查我的dto类是否有错误

ErrorDetails.java

public class ErrorDetails {
public ErrorDetails(Date timestamp, String message, String details) {
    super();
    this.timestamp = timestamp;
    this.message = message;
    this.details = details;
}
public Date getTimestamp() {
    return timestamp;
}
public void setTimestamp(Date timestamp) {
    this.timestamp = timestamp;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public String getDetails() {
    return details;
}
public void setDetails(String details) {
    this.details = details;
}
private Date timestamp;
private String message;
private String details;


 }
GlobalExceptionHandler.java

@ControllerAdvice
public class GlobalExceptionHandler {


@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> customValidationErrorHanding(MethodArgumentNotValidException exception){
    ErrorDetails errorDetails = new ErrorDetails(new Date(), "Validation Error!",
            exception.getBindingResult().getFieldError().getDefaultMessage());
    return new ResponseEntity<>(errorDetails,HttpStatus.BAD_REQUEST);
}


}
CustomerController.java

@RestController
public class CustomerController {


@Autowired
private CustomerService customerService;



@PostMapping("/addcustomer")
public Customer addCustomer(@Valid @RequestBody CustomerDto customer) {
    return customerService.save(customer);        
}
}
CustomerServiceImpl.java

@Transactional
@Service
public class CustomerServiceImpl implements CustomerService{

@Autowired
private CustomerRepository customerRepository;




@Override
public Customer save(CustomerDto customerdto){
    try {
        Customer customer = new Customer();
        convertToEntity(customer, customerdto);

        return customerRepository.save(customer);
    }
    
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    
    
}
CustomerRepository.java

@Repository
public interface CustomerRepository extends CrudRepository<Customer,String>{
}
我正在通过改装从kotlin连接到spring。例如,当我发布一个密码小于6位的客户时,我如何在kotlin中看到详细信息

        val test= Customer("aaaa","aaaaa","aaaaaaa","aaaaaa@gmail.com","1",1111)

    val req= serviceNew.addCustomer(test)

    req.enqueue(object : Callback<String> {
        override fun onFailure(call: Call<String>, t: Throwable) {
            println(t.message)
            println("fail")
        }
        override fun onResponse(call: Call<String>, response: Response<String>) {
            if(response.isSuccessful) {
                println(response.body())
            }
            else
            {
                //println(????)
            }

        }

    })
这就是我在kotlin发布客户帖子的方式

        val test= Customer("aaaa","aaaaa","aaaaaaa","aaaaaa@gmail.com","1",1111)

    val req= serviceNew.addCustomer(test)

    req.enqueue(object : Callback<String> {
        override fun onFailure(call: Call<String>, t: Throwable) {
            println(t.message)
            println("fail")
        }
        override fun onResponse(call: Call<String>, response: Response<String>) {
            if(response.isSuccessful) {
                println(response.body())
            }
            else
            {
                //println(????)
            }

        }

    })
val测试=客户(“aaaa”、“aaaaa”、“aaaaaaaaa”aaaaaa@gmail.com","1",1111)
val req=serviceNew.addCustomer(测试)
请求排队(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
println(t.message)
println(“失败”)
}
覆盖fun onResponse(调用:调用,响应:响应){
if(response.issucessful){
println(response.body())
}
其他的
{
//println(??)
}
}
})

您可以使用方法
response.message()
response.code()
response.errorBody()
获取响应消息或代码。以下是改装响应的文档:


在您的例子中,您可以使用
response.errorBody().string()
获取原始错误正文字符串,然后将此字符串转换为JSONObject,并使用
.getString(“详细信息”)
方法获取详细信息。

谢谢。是的
        val test= Customer("aaaa","aaaaa","aaaaaaa","aaaaaa@gmail.com","1",1111)

    val req= serviceNew.addCustomer(test)

    req.enqueue(object : Callback<String> {
        override fun onFailure(call: Call<String>, t: Throwable) {
            println(t.message)
            println("fail")
        }
        override fun onResponse(call: Call<String>, response: Response<String>) {
            if(response.isSuccessful) {
                println(response.body())
            }
            else
            {
                //println(????)
            }

        }

    })