Hello I want to Store JAXBelement value to String variable how to do this kindly tell me please.
I have Following method .
public JAXBElement<String> getSessionId()
{
return sessionId;
}
above method is in sessiondata class. i have create object of sessiodata class like below.
sessiondata result=new sessiondata();
i have call getsession method of sessiondata class like below.
result.getssionId();
now i want to store sessionid in to string variable. but its type is JAXBelment now tell me how to store in to String.
now i want to store sessionId to One String variable. and pass in to another method. kindly help me.
String sID = (String)result.getSessionId().getValue();
Related
I'm currently trying to deserialize an enum value from a json to an object containing a string(where the enum value should end up).
Example:
Domain class
public class Person {
private UUID personId;
private Occupation occupation;
}
Occupation class:
public class Occupation {
private String occupationType;
}
The code I am running is:
PersonResponse personResponse = JsonConverter.fromJson(message.getPayload(), new TypeReference<Person>() {
});
And the JSON is:
{"personId":"719e622e-6e00-4e84-b748-739f95d7c0fa", "occupationType":"STATE_EMPLOYEE"
Basically, I want my STATE_EMPLOYEE.name() value to end up in a usable object of the Occupation class. As it is now it tries to deserialize the value STATE_EMPLOYEE into an object of the Occupation class, which obviously doesn't work.
Is there a way for me to return an object like this? I don't want to change my Person object to hold an OccupationType enum because it has a load of other stuff as well.
The error I receive is:
Can not construct instance of person.package.Occupation: no String-argument constructor/factory method to deserialize from String value ('STATE_EMPLOYEE')
It basically fails trying to put my enum value into my Occupation object containing the string. (Where I want my thing to be).
Thanks in advance!
I solved it by creating my own custom Deserializer.
I have a Model Active Admin, I have created String ID setter and getter.
When I use setID in Login Form, I use this :
ActiveAdmin AA = new ActiveAdmin();
AA.setId(txtIdAdmin.getText());
When I test getter from login form, it works. When I test in another form, in another Java class, in different file, I can't get my string ID in Active Admin. I used:
AA.getId();
And the result is blank.
Build ActiveAdmin as a Singleton class, such that you have one instance for the entire application. Otherwise you will build a new object ActiveAdmin everytime when you use new ActiveAdmin().
Check link Java Singleton
Muhamad, setters and getters for properties typically take this form, based on what I think you're saying:
public class ActiveAdmin
{
public string Id { get; set; }
}
From another class, you would say "aA = new ActiveAdmin();"
Then aA.Id = "2"; and string aAId = aA.Id;
I have Java POJO object and my goal is to convert it to URL parameters and use it in POST method.
...
public class PayseraRequest {
private int projectid = 123;
private int orderid = 987;
private String accepturl = "http://www.test.com";
...
My goal is convert object PayseraRequest to String urlParams
urlParams -> projectid=123&orderid=987&http%3A%2F%2Fwww.test.com&...`
Yes, write a method to do this, but you should URLEncode each parameter. projectid and orderid do not need URLencoding but it doesn't hurt. accepturl must definitely be UrlEncoded. It is good practice to encode anything you want to put into the query string of a URL.
See https://docs.oracle.com/javase/7/docs/api/index.html?java/net/URLEncoder.html
you can override the toString method of that class and with a say so StringBuilder get what you need.
You can check an example I have here:
https://github.com/lmpampaletakis/datumBoxSpringMVC/tree/master/datumBoxSpringMVC/src/main/java/com/lebab/datumbox
Your answer might be at SendRequest.java
You can replace the values of each parameter you want from your pojo
i am very new to JAVA 8 and SPRING MVC . I have a java bean which is a POJO with setter and getter. My Spring web service using reflection maps the request parameters to the POJO.
I want to do input validation using annotation. I have a requirement were i need to read all the values of the annotated field and check atleast one value is provided. I wrote a sample code.... BUT NOT SURE HOW TO GET THE VALUES THAT ARE ASSIGNED TO A FIELD. Please do share sample code if you have:
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
boolean canProceed = false;
for(Field field : DocumentSearchRequest_global.class.getDeclaredFields())
{
if (field.isAnnotationPresent(ValidDocumentModifiedDate.class))
{
String name = field.getName();
//IAM ABLE TO GET THE NAME OF THE FIELD
System.out.println("1.name : "+ name);
System.out.println("2. "+field.getType().getName());
}
}
// Method[] method = DocumentSearchRequest_global.class.getDeclaredMethods();
for (Method method :DocumentSearchRequest_global.class.getDeclaredMethods() )
{
System.out.println(method.getName() );
//ABLE TO GET NAME OF THE GETTER AND SETTER METHODS IN THE POJO
//CAN U SUGGEST HOW TO READ THE VALUE OF A PARTICULAR FIELD.. EITHER BY //GETTING THE VALUE FROM THE GET METHOD??? ...
}
You can get the values by calling method.invoke(Object, Object...) where first parameter is your class instance on which method is to be executed and second variable arguments are arguments of the method. In your case it'll be null or empty. Here is simple code snippet Object value = method.invoke(DocumentSearchRequest_global_instance);
Here i am trying to get uContainer object from another project. uContainer having all the setters and getters with return values set from properties file. Like a user properties for perticular user. I am using to get perticular method values from uContainer instance. But in the 4th line my application getting crashed.
uContainer is an instance of UserContainer class.
getSingleResultListing also a boolean variable in UserContainer class having with getters and setters methods.
The code is given below.
Method getUContainer = form.getClass().getMethod("getUserContainer", new Class[0]);
Object uContainerObj = (Object)getUContainer.invoke(form, new Object[0]);
Method getFlagValueMethod = uContainerObj.getClass().getMethod("getSingleResultListing", new Class[0]);
String flagValue = (String)getFlagValueMethod.invoke(uContainerObj, new Object[0]);
log.info(">>>flagValue: "+flagValue);
boolean singleListingFlag = Boolean.getBoolean(flagValue);
log.info(">>>singleListingFlag: "+singleListingFlag);
here in the fourth line while invoking the uContainer object i am getting error ..
Thanks..
You are casting the returned object to a String, but you are not getting a String from that method. You cannot convert objects to String via a cast operator. If you want the string representation, write
String flagValue = getFlagValueMethod.invoke(uContainerObj, new Object[0]).toString();