I am using Retrofit to send and receive requests to my server.
I Have a model like below and I have to send it to my server but some variables in this model have not to send to the server.
public class SelectedListModel implements Serializable {
@SerializedName("p_id")
@Expose
private Long pId;
@SerializedName("p_qty")
@Expose
private Double pQty;
@Expose(serialize = false , deserialize = false)
private String pName; //Have not to send to server
@Expose(serialize = false , deserialize = false)
private String pPrice; //Have not to send to server
@Expose(serialize = false , deserialize = false)
private String pImageUrl; //Have not to send to server
}
and because of that, I am getting 400 in my responses from the server.
I used of @Expose(serialize = false, deserialize = false)
in order to Ignore variables that have not to send to the server.
But it doesn't work.
Is there any way to do this or I have to create another model just for my server?
To ignore the field elements simply add @Expose(deserialize = false, serialize = false) to your properties or none, and for (de)serialize your fields elements you can add the @Expose() annotations with empty values to your properties.
The @Body annotation defines a single request body. interface Foo { @POST("/jayson") FooResponse postJson(@Body FooRequest body); } Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request.
Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
You can use transient
keyword to ignore fields when requesting api calls
JAVA:
transient String name;
KOTLIN:
@Transient
var name: String
Use transient
keywork for that
public class SelectedListModel implements Serializable {
@SerializedName("p_id")
@Expose
private Long pId;
@SerializedName("p_qty")
@Expose
private Double pQty;
//@Expose(serialize = false , deserialize = false)
private transient String pName; //Have not to send to server
//@Expose(serialize = false , deserialize = false)
private transient String pPrice; //Have not to send to server
//@Expose(serialize = false , deserialize = false)
private transient String pImageUrl; //Have not to send to server
}
and no need to use @Expose(serialize = false , deserialize = false)
, into those fields which needed to be excluded.
Read Why does Java have transient fields? and Why use the `transient` keyword in java? for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With