Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何从用户那里获得@requestBody bean类字段的别名_Java_Spring_Spring Boot - Fatal编程技术网

Java 我如何从用户那里获得@requestBody bean类字段的别名

Java 我如何从用户那里获得@requestBody bean类字段的别名,java,spring,spring-boot,Java,Spring,Spring Boot,我在做一些springboot控制器。我正在公开一个PostAPI。因为我有@RequestBody OrdersBean作为参数。张贴Api代码如下: 控制器api post方法 @CrossOrigin(origins = "*", allowedHeaders = "*") @PostMapping(value="postOrder",consumes=MediaType.APPLICATION_JSON_VALUE)

我在做一些
springboot
控制器。我正在公开一个PostAPI。因为我有
@RequestBody OrdersBean
作为参数。张贴Api代码如下:

控制器api post方法

@CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value="postOrder",consumes=MediaType.APPLICATION_JSON_VALUE)
    private ResponseEntity<CoreResponseHandler> postOrder_(@RequestBody OrdersBean bean){
        long l_time_start = System.currentTimeMillis();
        Orders model = new Orders();
        Customer customer = cusRepo.findOne(bean.getCustomerId());
        if(customer==null) {
            return new  ResponseEntity<CoreResponseHandler>(new SuccessResponseBeanRefined(HttpStatus.NOT_FOUND, ResponseStatusEnum.FAILED, ApplicationResponse.Failed, "no such customer",commonsUtil.latency(l_time_start)+" ms"),HttpStatus.NOT_FOUND);          
        }
        User user = usrRepo.findOne(bean.getUserId());
        if(user==null) {
            return new  ResponseEntity<CoreResponseHandler>(new SuccessResponseBeanRefined(HttpStatus.NOT_FOUND, ResponseStatusEnum.FAILED, ApplicationResponse.Failed, "no such user",commonsUtil.latency(l_time_start)+" ms"),HttpStatus.NOT_FOUND);          
        }
        Payments payments = payRepo.findOne(bean.getPaymentsId());
        if(payments==null) {
            return new  ResponseEntity<CoreResponseHandler>(new SuccessResponseBeanRefined(HttpStatus.NOT_FOUND, ResponseStatusEnum.FAILED, ApplicationResponse.Failed, "no such payment",commonsUtil.latency(l_time_start)+" ms"),HttpStatus.NOT_FOUND);           
        }

        model.setCustomer(customer);
        model.setUser(user);
        model.setPayments(payments);
        model.setNotified(bean.getNotified());
        model.setOrderActive(bean.getOrderActive());
        model.setOrderDate(bean.getOrderDate());
        model.setOrderPayload(bean.getOrderPayload());
        model.setOrderStatus(bean.getOrderStatus());

        Orders model2 = ordRepo.saveAndFlush(model);
        if(model2!=null)
        return new  ResponseEntity<CoreResponseHandler>(new SuccessResponseBeanRefined(HttpStatus.OK, ResponseStatusEnum.SUCCESSFUL, ApplicationResponse.SUCCESSFUL, "order created",commonsUtil.latency(l_time_start)+" ms"),HttpStatus.OK);
        else
            return  new ResponseEntity<CoreResponseHandler>(new SuccessResponseBeanRefined(HttpStatus.BAD_REQUEST, ResponseStatusEnum.FAILED, ApplicationResponse.Failed,"exception",commonsUtil.latency(l_time_start)+" ms"),HttpStatus.BAD_REQUEST);
    }
在上面的bean类中,我有下面的字段

private int customer_Id; // I want user to use alias as cust.id
private int payments_Id; // I want user to use alias as pay.id
private int user_Id; // I want user to use alias as usr.id
用户在使用我的API时,是否可以按照上面的注释更改字段名。。如下
JsonObject

{
"cust.id" : 122,
"pay.id" : 678,
"usr.id" : 190,
.
.
.
}
是否有任何特殊注释可用于这些字段,为其提供用户别名。

是,执行此操作:

    @JsonProperty("cust.id")
    private int customerId;

    @JsonProperty("pay.id")
    private int paymentsId;

    @JsonProperty("usr.id")
    private int userId;
是的,通过这样做:

    @JsonProperty("cust.id")
    private int customerId;

    @JsonProperty("pay.id")
    private int paymentsId;

    @JsonProperty("usr.id")
    private int userId;