英文:
Successful response only for entity class fields
问题
这是我的实体类
public class Tran {
    @Id
    @NotNull
    @Column(unique = true)
    private String tranId;
    private String code;
    private String failureReason;
    @NotNull
    private Date transaDate;
    private String switchingId;
}
这是我的控制器类
public class TranController {
    @CrossOrigin(origins = "*")
    @PostMapping(value = "/pur", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String, Object>> createTran(@RequestHeader(required = false, name="request-id") String requestId,
                                                          @RequestHeader(required = false, name="datetime") String requestDate,
                                                          @RequestHeader(required = false, name= "channel") String requestChannel,
                                                          @RequestBody TransRequestEntity createTransactionRequest,
                                                          HttpServletRequest req) throws ApiBaseException {
        Tran tran = objectMapper.convertValue(createTranRequest, Tran.class);
        TransCreateResponseEntity tranCreateResponseEntity = tranCreateService.createTransaction(tran, requestId);
        Map<String, Object> trResponse = responseEntityTransformer.transform(transactionCreateResponseEntity, transCreateResponseTransformer);
        switch (transactionCreateResponseEntity.getTransactionResponseHeader().getResponseCode()) {
            case "00":
                return ResponseEntity.status(HttpStatus.OK).body(trResponse);
            case "10":
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(trResponse);
            default:
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(trResponse);
        }
    }
}
我发送这种类型的 JSON
{
  "tranId": "120",
  "code": "50ac",
  "transaDate": "2020/02/20",
  "switchingId": "464vc"
}
这个请求显示成功响应,没有问题。但是如果我发送以下请求:
{
  "tranId": "120",
  "code": "50ac",
  "transaDate": "2020/02/20",
  "switchingId": "464vc",
  "xyz": "test"
}
这也会显示成功响应,但我不需要 xyz 字段,这个字段也不在我的实体类中。如果我添加了不在实体类中的其他字段,我想显示错误消息,如"请求错误"。
谢谢。
英文:
Here is my entity class
public class Tran{
   @Id
   @NotNull
   @Column(unique = true)
   private String tranId;
   private String code;
   private String failureReason;
   @NotNull
   private Date transaDate;
   private String switchingId
}
This is my controller class
public class TranController{
    @CrossOrigin(origins = "*")
    @PostMapping(value = "/pur", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String,Object>> createTran(@RequestHeader(required = false,name="request-id") String requestId,
                                                @RequestHeader(required = false,name="datetime") String requestDate,
                                                @RequestHeader(required = false,name= "channel") String requestChannel,
                                                @RequestBody TransRequestEntity createTransactionRequest,
                                                HttpServletRequest req) throws ApiBaseException {
        Tran tran = objectMapper.convertValue(createTranRequest,Trans.class);
        TransCreateResponseEntity tranCreateResponseEntity = tranCreateService.createTransaction(tran,requestId);
        Map<String,Object> trResponse = responseEntityTransformer.transform(transactionCreateResponseEntity,transCreateResponseTransformer);
        switch (transactionCreateResponseEntity.getTransactionResponseHeader().getResponseCode()){
            case "00" :
                return ResponseEntity.status(HttpStatus.OK).body(trResponse);
            case "10" : return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(trResponse);
            default: return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(trResponse);
        }
    }
}
I send this type json
{
  "tranId": "120",
  "code": "50ac",
  "transaDate": "2020/02/20",
  "switchingId": "464vc",
}
This request show the success response. there are no problem
But i send request like that
{
  "tranId": "120",
  "code": "50ac",
  "transaDate": "2020/02/20",
  "switchingId": "464vc",
  "xyz" : "test"
}
this is also show successful response, but i don't need xyz field, this field also not in the my entity class..
I want to show error massage like "bad request"  if I add another field that is not in the entity class
Thank you
答案1
得分: 3
Spring使用Jackson库。默认情况下,框架使用的ObjectMapper将其FAIL_ON_UNKNOWN_PROPERTIES设置为false。
将以下内容放入您的application.properties文件中:
spring.jackson.deserialization.fail-on-unknown-properties=true
这将导致对未识别的属性抛出异常。如果您希望自己处理异常,可以使用以下方式:
@ExceptionHandler(UnrecognizedPropertyException.class)
更多信息请参考此处。
英文:
Spring uses the Jackson library. By default, the ObjectMapper used by the framework sets its FAIL_ON_UNKNOWN_PROPERTIES to false.
Put this in your application.properties: <br>
spring.jackson.deserialization.fail-on-unknown-properties=true
It will start failing for unrecognized properties. If you wish to handle the exception yourself, you can do so with
@ExceptionHandler(UnrecognizedPropertyException.class)
More here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论