can't response 'isOk' field in spring boot - java

i'm creating CreateOrUpdateProduct API use Spring boot. i want to return to consumer two fields ('message & isOk'). But when i exec this API, i received ('message & ok') fields. what's happened? please expand me. thanks advance!
this is my function
public ResponseBase CreateOrUpdateProduct(Product product) {
....
return responseBase;
}
public class ResponseBase {
boolean isOk;
public boolean isOk() {
return isOk;
}
public void setOk(boolean isOk) {
this.isOk = isOk;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
String message;
}
i received
{
"message":null,
"ok": true
}

I think your answer is here: Jackson renames boolean field by removing is
Jackson (serializer) sees "isOk" as a get method of a boolean variable named "ok". This is a common naming pattern developers use on get methods for boolean variables.
EDIT:
You shouldn't set the name of your method to "getIsOk", because that doesn't follow the naming convention of get method for boolean variables. This is not a very good solution, but it'll work.
Jackson provides an annotation to you that set the name of the serialized variable:
#JsonProperty(value="isOk")
public boolean isOk() {
return isOk;
}

You should rename your getter to getIsOk().
It will return the expected answer :
{
"message":null,
"isOk": true
}

Related

Setting a boolean value in Constructor, changing it in a method, return it in another

I'm a newbie yet, so please feel free to accuse me of asking silly things xD. I just started coding. So I want to specify my question to make it clear for you. I'm stuck regarding this: We need a constructor (public DoggoII) which sets our value to false. Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later. Consider the rest of the code as done. Can someone give me a hint or something on how to do that? Because I'm kinda lost.
The question is if I have a fault that I can't find and how to return a boolean value in another method in general. Thanks!
public class Doggo {
private String name;
private boolean goodBoi;
public Doggo(String name){
this.name = name;
}
public String getName() {
return name;
}
public void makeBark() {
System.out.println(name + " said: Woof woof");
}
public Doggo (boolean goodBoi){
this.goodBoi= false;
}
public void makeGoodBoi(){
this.goodBoi = !this.goodBoi;
}
public void isGoodBoi(){
if (makeGoodBoi()){
return;
}
}
public void whosAGoodBoi() {
if (isGoodBoi()) {
System.out.println(name + " is such a good boii");
} else {
System.out.println(name + " is not a good boi :(");
}
}
public static void main(String[] args) {
Doggo dog = new Doggo("Casper");
System.out.println(dog.getName());
dog.makeBark();
}
}
Just a basic getter, use boolean as return type instead of void.
public boolean isGoodBoi() {
return goodBoi;
}
Since goodBoi is a class member and by default boolean class members are false, so you don’t have to do anything except add a getter
public boolean isGoodBoi() {
return goodBoi;
}
This will send whatever current value of class member is.
So getting this would be as simple as;
DOGGO_OBJECT.isGoodBoi();
Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later.
public void makeGoodBoi() {
this.goodBoi = true;
}

Is it possible to create enums with a variable which is defined only when using this enum?

I am trying to build an object which will be used to communicate with other objects. This communication object should have an enum, these enums include "ORDER_CREATED", "ORDER_CANCELLED" and "ORDER_AT_LOCATION". What I now want to do is assign a value to the enum "ORDER_AT_LOCATION" with a String location. This way there are no blank fields containing "null" if the enum is "ORDER_CREATED".
I am not entirely sure if this is even possible.
I tried using a constructor for one specific enum but didn't have any luck
public class OrderEvent {
private OrderEventTypes eventType;
}
enum OrderEventTypes{
//I want a constructor with String info only for the enum ORDER_AT_LOCATION
ORDER_AT_LOCATION{
String info;
public void ORDER_IN_TRANSIT(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
}
, ORDER_CANCELLED, ORDER_COMPLETED, ORDER_CREATED;
}
the expected result should be that this statement works.
if(eventType.equals("ORDER_AT_LOCATION")) {
System.out.println(eventType.getInfo());
}
if you have any other nice solutions to this I would be very thankful, all the solutions I could come up with were perfectly do-able but not as nice as this would be.
You can create the enum with a constructor which accepts a String value and initialises the info field:
enum OrderEventTypes{
ORDER_AT_LOCATION("ORDER_AT_LOCATION"),
ORDER_CANCELLED,
ORDER_COMPLETED,
ORDER_CREATED;
private final String info;
OrderEventTypes(){
this("");
}
OrderEventTypes(String info){
this.info = info;
}
public String getInfo(){
return this.info;
}
#Override
public String toString() {
return this.info;
}
}
Now when you need to compare, you can use the getter to get the value of the info field:
if("ORDER_AT_LOCATION".equals(eventType.getInfo())){
System.out.println(eventType.getInfo());
}

How to get enum value from property

I have an enum with values VALID and INVALID, which have a boolean property associated with them. I would like to get the enum value based on a boolean value I provide.
If it is true I should get VALID, if it is false I should get INVALID. I would like to do so in a getter method like the below, based on the value of the member variable
public boolean getCardValidityStatus() {
return CardValidationStatus status = CardValidationStatus(this.mCardValidityStatus));
}
My code:
private enum CardValidationStatus {
VALID(true),
INVALID(false);
private boolean isValid;
CardValidationStatus(boolean isValid) {
this.isValid = isValid;
}
public boolean getValidityStatus() {
return this.isValid;
}
}
You're able to achieve that using a static lookup method in the enum itself:
private enum CardValidationStatus {
VALID(true),
INVALID(false);
//...
public static CardValidationStatus forBoolean(boolean status) {
//this is simplistic given that it's a boolean-based lookup
//but it can get complex, such as using a loop...
return status ? VALID : INVALID;
}
}
And the appropriate status can be retrieved using:
public CardValidationStatus getCardValidityStatus() {
return CardValidationStatus.forBoolean(this.mCardValidityStatus));
}
I would add a parse method to your enum, which takes the boolean, iterates over all the values and returns the one that matches, for example:
public CardValidationStatus parse(boolean isValid) {
for (CardValidationStatus cardValidationStatus : CardValidationStatus.values()) {
if (cardValidationStatus.getValidityStatus() == isValid) {
return cardValidationStatus;
}
}
throw new IllegalArgumentException();
}
#ernest_k solution made this work, but I think that's not reliable solution.
You should always do code which is independent.
Because his solution is hardcoded. What if values of VALID & INVALID are changed. Will you change your forBoolean logics also?
Because he did not check what the Enum fields are holding inside it.
Reliable solution will be #DaveyDaveDave answer. This will also work when you have many status with VALID & INVAlID.
private enum CardValidationStatus {
VALID(true),
INVALID(false);
//...
public CardValidationStatus forBoolean(boolean isValid) {
for (CardValidationStatus cardValidationStatus : CardValidationStatus.values()) {
if (cardValidationStatus.getValidityStatus() == isValid) {
return cardValidationStatus;
}
}
throw new IllegalArgumentException();
}
}
Suggestion (Easiest way I think)
Why are you making Enum just for storing 2 boolean values?
Just make static boolean named by VALID & INVALID.
public static final boolean CARD_STATUS_VALID = true;
public static final boolean CARD_STATUS_INVALID = false;
if(cardStatus == CARD_STATUS_VALID){
// todo
}

JSON Objectmapper issue

Getting both java obejct as will jsonproperty is getting generated whilc I convert java object to JSON.
Can you please confirm where I have made mistake.
Add #JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE) to your class:
#JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE)
#JsonInclude(JsonInclude.Include.NON_EMPTY)
public class FalconidePersonalizationVO {
By default Jackson follow java bean convention to output json properties. As result, it founds your getX method and output xapiheader property.
But you also annotate your field with #JsonProperty so another property named x-apiheader is also ouputed.
Disable getterX detection method will prevent jackson output getter fields.
**************** Solution 1 ****************
Annotate getter / setter with #JsonProperty as well (now annotating field is not mandatory)
public class FalconidePersonalizationVO {
#JsonProperty("x-apiheader-cc")
private String xApiheaderCc;
#JsonProperty("x-apiheader")
private String xApiheader;
#JsonProperty("x-apiheader-cc")
public String getXApiheaderCc() {
return xApiheaderCc;
}
#JsonProperty("x-apiheader-cc")
public void setXApiheaderCc(String xApiheaderCc) {
this.xApiheaderCc = xApiheaderCc;
}
#JsonProperty("x-apiheader")
public String getXApiheader() {
return xApiheader;
}
#JsonProperty("x-apiheader")
public void setXApiheader(String xApiheader) {
this.xApiheader = xApiheader;
}
}
**************** Solution 2 ****************
Follow setter / getter naming convention. In normal naming convention first letter of field name is capitalized prepending it with set / get. But in this case since second char is capital so, first char is not capitalized. See https://stackoverflow.com/a/16146215/3295987
public class FalconidePersonalizationVO {
#JsonProperty("x-apiheader-cc")
private String xApiheaderCc;
#JsonProperty("x-apiheader")
private String xApiheader;
/*
* Setter / getter auto generated in eclipse
*/
// getXApiheaderCc -> getxApiheaderCc
public String getxApiheaderCc() {
return xApiheaderCc;
}
public void setxApiheaderCc(String xApiheaderCc) {
this.xApiheaderCc = xApiheaderCc;
}
public String getxApiheader() {
return xApiheader;
}
public void setxApiheader(String xApiheader) {
this.xApiheader = xApiheader;
}
}

Guava Collections filter

I am just starting with Guava collections and am trying to write a predicate for a list. I am using Guava 11 since I am on java 5.
Here is my first effort....
public abstract class StatusBean {
public enum Status {GREEN, AMBER, RED, BLUE};
}
public class RegisterReplicationSynchTime {
private Status status = null;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
public class RegisterReplicationSynchTime {
public void doFilter() {
List<RegisterReplicationSynchTime> registerReplicationSynchTimes = dao.getMyList();
Predicate<StatusBean.Status> predicate = new Predicate<StatusBean.Status>() {
public boolean apply(StatusBean.Status status) {
return status != StatusBean.Status.GREEN;
}
};
// !!!! COMPILER DOES NOT LIKE THIS LINE!!!!!!
Collections2.filter(registerReplicationSynchTimes, predicate);
}
}
The compiler does not like the call I am making to filter and it is giving me an error as per below.
The method filter (Collection<E>,<Predicate<? super E>) in the type
Collections 2 is not applicable for the arguements
(List<RegisterReplicationSynchTime>,Predicate<StatusBean.Status>)
I am not sure what I need to do to get it right. Can someone please give me a hand?
As the error message tells you, there is a mismatch between the type of elements in your List (RegisterReplicationSynchTime) and the elements processed by the predicate (StatusBean.Status).
Perhaps you want to change your code to filter a List<StatusBean.Status>?

Categories