如何从用户那里期望@requestBody bean类字段的别名?

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

how can I expect alias names for @requestBody bean class fields from user

问题

In the OrdersBean class, you can use the @JsonProperty annotation from the Jackson library to specify alias names for the fields when serializing or deserializing JSON. Here's how you can do it for your fields:

import com.fasterxml.jackson.annotation.JsonProperty;

public class OrdersBean implements java.io.Serializable {

    // Other fields...

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

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

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

    // Getters and setters...

}

With this annotation, when you serialize an instance of OrdersBean to JSON, it will use the specified alias names for these fields, allowing users to send JSON with those alias names when consuming your API.

英文:

I was working on some spring-boot controller. I am exposing a post API. In that I have @RequestBody OrdersBean bean as parameter. Post Api code below:

Controller api post method

@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);
	}

OrderBean class

import java.util.Date;
public class OrdersBean implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7567253331511523847L;
	
	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
	private String orderPayload;
	private Date orderDate;
	private String orderStatus;
	private Byte notified;
	private Byte orderActive;

	public OrdersBean_() {
	}

	public OrdersBean_(int customer_Id, int payments_Id, int user_Id, String orderPayload, Date orderDate,
			String orderStatus, Byte notified, Byte orderActive) {
		this.customer_Id = customer_Id;
		this.payments_Id = payments_Id;
		this.user_Id = user_Id;
		this.orderPayload = orderPayload;
		this.orderDate = orderDate;
		this.orderStatus = orderStatus;
		this.notified = notified;
		this.orderActive = orderActive;
	}

	public int getCustomer_Id() {
		return customer_Id;
	}

	public void setCustomer_Id(int customer_Id) {
		this.customer_Id = customer_Id;
	}

	public int getPayments_Id() {
		return payments_Id;
	}

	public void setPayments_Id(int payments_Id) {
		this.payments_Id = payments_Id;
	}

	public int getUser_Id() {
		return user_Id;
	}

	public void setUser_Id(int user_Id) {
		this.user_Id = user_Id;
	}

	public String getOrderPayload() {
		return orderPayload;
	}

	public void setOrderPayload(String orderPayload) {
		this.orderPayload = orderPayload;
	}

	public Date getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	public String getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(String orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Byte getNotified() {
		return notified;
	}

	public void setNotified(Byte notified) {
		this.notified = notified;
	}

	public Byte getOrderActive() {
		return orderActive;
	}

	public void setOrderActive(Byte orderActive) {
		this.orderActive = orderActive;
	}


}

in above bean class I have below fields

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

Is it possible for user when consuming my API, he can change the field names as provided in comments above.. like below JsonObject

{
"cust.id" : 122,
"pay.id" : 678,
"usr.id" : 190,
.
.
.
}

Is there any special annotations that I can use for those fields to give them alias names for user.

答案1

得分: 3

"是的,通过这样做:

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

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

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

<details>
<summary>英文:</summary>

yes by doing this:

@JsonProperty(&quot;cust.id&quot;)
private int customerId;

@JsonProperty(&quot;pay.id&quot;)
private int paymentsId;

@JsonProperty(&quot;usr.id&quot;)
private int userId;

</details>



huangapple
  • 本文由 发表于 2020年7月31日 21:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192969.html
匿名

发表评论

匿名网友

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

确定