只返回实体类字段的成功响应

huangapple go评论150阅读模式
英文:

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 = &quot;*&quot;)
    @PostMapping(value = &quot;/pur&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity&lt;Map&lt;String,Object&gt;&gt; createTran(@RequestHeader(required = false,name=&quot;request-id&quot;) String requestId,
                                                @RequestHeader(required = false,name=&quot;datetime&quot;) String requestDate,
                                                @RequestHeader(required = false,name= &quot;channel&quot;) String requestChannel,
                                                @RequestBody TransRequestEntity createTransactionRequest,
                                                HttpServletRequest req) throws ApiBaseException {



        Tran tran = objectMapper.convertValue(createTranRequest,Trans.class);

        TransCreateResponseEntity tranCreateResponseEntity = tranCreateService.createTransaction(tran,requestId);

        Map&lt;String,Object&gt; trResponse = responseEntityTransformer.transform(transactionCreateResponseEntity,transCreateResponseTransformer);

        switch (transactionCreateResponseEntity.getTransactionResponseHeader().getResponseCode()){
            case &quot;00&quot; :
                return ResponseEntity.status(HttpStatus.OK).body(trResponse);
            case &quot;10&quot; : return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(trResponse);
            default: return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(trResponse);
        }
    }
}

I send this type json

{
  &quot;tranId&quot;: &quot;120&quot;,
  &quot;code&quot;: &quot;50ac&quot;,
  &quot;transaDate&quot;: &quot;2020/02/20&quot;,
  &quot;switchingId&quot;: &quot;464vc&quot;,
}

This request show the success response. there are no problem
But i send request like that

{
  &quot;tranId&quot;: &quot;120&quot;,
  &quot;code&quot;: &quot;50ac&quot;,
  &quot;transaDate&quot;: &quot;2020/02/20&quot;,
  &quot;switchingId&quot;: &quot;464vc&quot;,
  &quot;xyz&quot; : &quot;test&quot;
}

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

huangapple
  • 本文由 发表于 2020年8月6日 12:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63276908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定