#ApiModelProperty(hidden = true) is ignored if the property is a Class. My problem is that it expands too much and a leaf field becomes required.
Is there a way to make it work? Or the only solution is to make a different class for request usages?
A simple example:
public class Child {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Parent {
private String name;
#ApiModelProperty(hidden = true)
private Child child;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
If I put #ModelAttribute over Parent, in rest endpoint swagger will render the child.name field in the request.
Related
I'm playing a little bit with Optional to understand how it works. Let's say I have this class:
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and I want to return a Student by Id or Optional.empty if it doesn't find it. This is what I have so far:
public class Main {
static List<Student> students = new ArrayList<>();
public static void main(String[] args) {
students.add(new Student(1, "name1"));
students.add(new Student(2, "name2"));
students.add(new Student(3, "name3"));
System.out.println(getStudentById(1).get().getName());
}
public static Optional<Student> getStudentById(int id) {
return students
.stream()
.filter( s -> s.getId() == id)
.findFirst();
}
}
That works but I wanted to add this line:
.findFirst()
.orElse(Optional.empty());
and I got this:
Error:(23, 39) java: incompatible types: no instance(s) of type variable(s) T exist so that java.util.Optional conforms to com.company.Student
Also I'd like to know if that is the correct way to go over a list, I mean element by element or there is something better?
If you read javadocs of Stream#findFirst() you will find that you already have what you need:
Returns an Optional describing the first element of this stream, or an
empty Optional if the stream is empty. If the stream has no encounter
order, then any element may be returned.
So just do
return students
.stream()
.filter( s -> s.getId() == id)
.findFirst();
I am developing a chat app but I am getting the following error: Cannot find getter for the field.
that's why I want to convert Integer to String using type converters in Room but I did not find any sample below my User.java model class
#Entity public class User implements IChatUser {
#PrimaryKey(autoGenerate = true)
private Integer id;
#ColumnInfo(name = "name")
private String name;
#Ignore
Bitmap icon;
public User() {
}
#Ignore
public User(String name, Bitmap icon) {
this.name = name;
this.icon = icon;
}
#Override
public Integer getId() {
return this.id;
}
#Override
public String getName() {
return this.name;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
#Override
public Bitmap getIcon() {
return this.icon;
}
#Override
public void setIcon(Bitmap icon) {
this.icon = icon;
} }
below IchatUser.kt
interface IChatUser {
fun getId(): String
fun getName(): String?
fun getIcon(): Bitmap?
fun setIcon(bmp: Bitmap)
}
To create a #TypeConverter create a java class and call it IntConverter
public class IntConverter {
#TypeConverter
public static String toString(int number) {
return number == null ? null : Integer.toString(number);
}
#TypeConverter
public static int toInt(String str) {
return str == null ? null : Integer.parseInt(str);
}
}
Now you need the database to know about this TypeConverter so declare this:
#TypeConverters(IntConverter.class)
Just below the #Database annotation in the Database class
The value returned by the JSP page for the path constituency is NULL. I have opted for a drop down list to display all the possible constituencies that can be selected. I have elaborated on the problem below.
Here is the Person Class:
#Entity
#Table(name="person")
public class person{
#Id
#Column(name = "NIC")
private Integer NIC;
#Column(name= "firstname")
private String fname;
#Column(name= "lastname")
private String lname;
#ManyToOne
#JoinColumn(name= "constituency_id")
private constituency constituency;
#OneToOne
#JoinColumn(name="NIC")
private login log;
public person(){
this.NIC = 1234;
this.fname = "Undefined";
this.lname = "Undefined";
}
public person(String fname, String lname, Integer NIC) {
this.fname = fname;
this.lname = lname;
this.NIC = NIC;
}
public Integer getNIC() {
return this.NIC;
}
public void setNIC(Integer NIC) {
this.NIC = NIC;
}
public String getfname() {
System.out.println(this.fname);
return this.fname;
}
public void setfname(String name) {
this.fname = name;
}
public String getlname() {
return this.lname;
}
public void setlname(String lname) {
this.lname = lname;
}
public constituency getConstituency() {
return this.constituency;
}
public void setConstituency(constituency id) {
this.constituency = id;
}
public login getlogin() {
return this.log;
}
public void setlogin(login log) {
this.log = log;
}
#Override
public String toString() {
return this.lname + " : " + this.fname;
}
}
Here is the Constituency Class:
#Entity
#Table(name="constituency")
public class constituency {
#Column(name="address")
private String address;
#Column(name="name")
private String name;
#Id
#Column(name="constituency_id")
private Integer constituency_id;
#Column(name="noofvoters")
private Integer voters;
#OneToMany(mappedBy="constituency", cascade = {CascadeType.ALL})
private Set <person> persons;
public constituency(){
this.constituency_id = 0;
this.name = "Undefined";
this.address = "Undefined";
this.voters = 0;
}
public constituency(String name, String address, Integer voters, Integer id) {
this.name = name;
this.address = address;
this.voters = voters;
this.constituency_id = id;
}
public String getname() {
System.out.println(this.name);
return this.name;
}
public void setname(String name) {
this.name = name;
}
public String getaddress() {
System.out.println(this.address);
return this.address;
}
public void setaddress(String name) {
this.address = name;
}
public Integer getvoters() {
return this.voters;
}
public void setvoters(Integer voters) {
this.voters = voters;
}
public Integer getconstituency_id() {
return this.constituency_id;
}
public void setconstituency_id(Integer id) {
this.constituency_id = id;
}
#Override
public String toString() {
return this.name;
}
}
Here is the portion of the controller responsible for handling the operation:
#RequestMapping(value="/Add", method=RequestMethod.GET)
public String add(Model model){
List <constituency> constit = constituencyDAO.details();
model.addAttribute("message", "Add a person for voting");
model.addAttribute("per", new person());
model.addAttribute("constituency", constit);
return "Add";
}
#RequestMapping(value="/Add", method=RequestMethod.POST)
public ModelAndView addperson(#ModelAttribute("per") person per, BindingResult bind){
System.out.println("In controller");
System.out.println(per.getfname()+" First-name");
System.out.println(per.getConstituency()+" constituency");
return hello();
}
Finally, this is the portion of the JSP page with the form:select tag:
<div class = "form-group">
<label for = "element1" class="control-label col-xs-2">Constituency ID</label>
<div class="col-xs-10">
<form:select path="constituency" name="constituency">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${constituency}" itemValue="constituency_id" itemLabel="name" />
</form:select>
<p>Constituency ID</p>
</div>
</div>
Now, the problem is that even though the JSP page is able to successfully display the constituency attribute in the form:options, when the "per" model attribute is retrieved in the controller, the value for constituency and only constituency is NULL.
Below is the output in Eclipse.
The Form:
The output in the RequestMethod.POST:
I know I've already asked about Lab 14, but I've edited my code a lot since then. I think its a lot better, but I think something is still wrong. Is it with the setters? I looked on another forum that says I'm making progress, but while they get back to me, I need someone to help edit my syntax. If what I'm doing wrong is my setters, I'll be fine but tell me that please.
I really really really really want to get this program working so that I can move on since I'm behind in class. Yes, this is Java.
Here's my code:
public class StreetAddress
{
public String street;
public String city;
public String state;
public String zip;
public StreetAddress(String findStreet, String findCity,String findState,String findZIP) {
street = findStreet;
city = findCity;
state = findState;
zip = findZIP;
}
public String getStreet() {
return street;
}
public void makeStreet(String street) {
this.street = street;
}
public String getcity() {
return city;
}
public void makeCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void makeState(String state) {
this.state = state;
}
public String getZip() {
return zip;
public void makeZip(String zip) {
this.zip = zip;
}
}
}
}
Thanks.
You can do it in any of the 2 ways.
Using parameterized constructor
class StreetAddress
{
public String street;
public String city;
public String state;
public String zip;
public StreetAddress(String findStreet, String findCity,String findState,String findZIP){
street = findStreet;
city = findCity;
state = findState;
zip = findZIP;
}
public String getStreet() {
return street;
}
public String getcity() {
return city;
}
public String getState(){
return state;
}
public String getZip() {
return zip;
}
}
and pass values from main function during calling of StreetAddress object.
by using Default constructor,
class StreetAddress
{
public String street;
public String city;
public String state;
public String zip;
public String getStreet() {
return street;
}
public void makeStreet(String street){
this.street = street;
}
public String getcity() {
return city;
}
public void makeCity(String city){
this.city = city;
}
public String getState(){
return state;
}
public void makeState(String state){
this.state = state;
}
public String getZip() {
return zip;
}
}
and pass values directly by calling functions makeZip(anyString);
You have three closing braces at the end! It should be only one and you forgot to write closing brace for your getZip Method. One more problem with your code is that you have defined your instance variables public which doesn't make any sense when you are implementing accessors for them. As a result, you'd better to make them private so you also follow the encapsulation topic of OOP. One other problem with your naming is your setters' naming convention. It is a common convention in Java programming that you name your getter as get[InstanceVatiableName] ie: getStreet and your setter like: set[InstanceVariableName] ie: setStreet.
I have corrected mentioned problems for your code in below:
public class StreetAddress
{
private String street;
private String city;
private String state;
private String zip;
public StreetAddress(String findStreet, String findCity, String findState, String findZIP) {
street = findStreet;
city = findCity;
state = findState;
zip = findZIP;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getcity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
I am trying to access getter properties in a subclass with a TableView in JavaFX. I have the following class:
public class PersonType implements Serializable {
private static final long serialVersionUID = 1L;
Person person;
short count;
public PersonType() {
}
public PersonType(Person person, short count) {
super();
this.person = person;
this.count = count;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public short getCount() {
return count;
}
public void setCount(short count) {
this.count = count;
}
Person is like this:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
String firstName;
String lastName;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Okay - lastly we have the following:
#FXML
private TableColumn tcFirstName;
#FXML
private TableColumn tcLastName;
#FXML
private TableView tblPersonTypes;
ArrayList<PersonType> pType = new ArrayList<PersonType>();
//Can assume that pType here has say 5 entries, the point of this
//is I'm trying to get to the firstName, lastName properties of the
//PersonType in the TableView below like the following:
tcFirstName.setCellValueFactory(new PropertyValueFactory<String,String>("firstName"));
tcLastName.setCellValueFactory(new PropertyValueFactory<String,String>("lastName"));
//Populate Table with Card Records
ObservableList<PersonType> data = FXCollections.observableArrayList(pType);
tblPersonTypes.setItems(data);
And I'm unsure how with a list of PersonTypes I can tell the table columns that I want the firstName and lastName properties of the Person object contained within. I know I could create a new object, and have the "count" from PersonTypes, then the other properties of "firstName", "lastName" etc without having an object property of Person. Any help would be greatly appreciated.
-- EDIT --
Another way I thought to do this was using CellFactories - where I would pass in to the CellValueFactories the Person object, then set the CellFactory to return a String value (firstName for the first name column, etc). And it would look like this:
tcFirstName.setCellValueFactory(new PropertyValueFactory<Person,String>("person"));
tcFirstName.setCellFactory(new Callback<TableColumn<Person,String>,TableCell<Person,String>>(){
#Override
public TableCell<Person,String> call(TableColumn<Person,String> param) {
TableCell<Person,String> cell = new TableCell<Person,String>(){
#Override
public void updateItem(String item, boolean empty) {
if(item!=null){
setGraphic(new Label(item.getFirstName()));
}
}
};
return cell;
}
});
Try this:
tcFirstName.setCellValueFactory(new Callback<CellDataFeatures<PersonType, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<PersonType, String> p) {
// p.getValue() returns the PersonType instance for a particular TableView row
if (p.getValue() != null && p.getValue().getPerson() != null) {
return new SimpleStringProperty(p.getValue().getPerson().getFirstName());
} else {
return new SimpleStringProperty("<No TC firstname>");
}
}
});
}