I am trying to add default values to property attributes.I have one class inside which i have other class type injected as list.
I am able to get the default values for all attributes even on dependent class.I want to know is there any way using #value to add one more list of default values of custom objects.
My model classes are-
package com.example.test.Model;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Employee {
#Value("1")
private Integer id;
#Value("Anubham")
private String name;
#Autowired
private List<Departments>departments;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Departments> getDepartments() {
return departments;
}
public void setDepartments(List<Departments> departments) {
this.departments = departments;
}
public Employee() {
super();
}
public Employee(Integer id, String name, List<Departments> departments) {
super();
this.id = id;
this.name = name;
this.departments = departments;
}
#Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", departments=" + departments + "]";
}
}
Another one is:
package com.example.test.Model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Departments {
#Value("1")
private int id;
#Value("computer")
String subject;
public Departments() {
super();
}
public Departments(int id, String subject) {
super();
this.id = id;
this.subject = subject;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
#Override
public String toString() {
return "Departments [id=" + id + ", subject=" + subject + "]";
}
}
I am getting output as Employee [id=1, name=Anubham, departments=[Departments [id=1, subject=computer]]].
I want to have one more record for departments field.
I wonder is it possible using #value without using any other way.
In your example "Departments" is a bean that is injected into Employee bean. If you want to have multiple departments, you have to create interface/abstraction "Department" and implement it in beans with concrete values that you want (DepartmentA, DepartmentB).
But Value annotation is not meant do inject static content but rather values from properties files. I don't know what you want to achieve this way.
Related
for instance, I have a table with different locations and their description too. Now I need to print the locations which have descriptions on one page or in one box and simultaneously the locations which do not have descriptions should be displayed on another page.
NOTE:
this has to be done by using spring concept only
for reference, I was able to print the locations which have no descriptions in the below fashion.
entity
package com.test.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
#Entity
#Table(name= "loc_dtls")
public class Location {
#Id
#GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY)
private long id;
#Column(name="place_name")
private String placeName;
#Column(name="place_description")
private String placeDesc;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
public String getPlaceDesc() {
return placeDesc;
}
public void setPlaceDesc(String placeDesc) {
this.placeDesc = placeDesc;
}
#Override
public String toString() {
return "Location [id=" + id + ", placeName=" + placeName + ", placeDesc=" + placeDesc + "]";
}
}
controller
#GetMapping("/")
public String singleElement(Model m) {
List<Location> util = locRepo.findByplaceDesc(null);
m.addAttribute("unlist_places", util);
return "index";
}
repository
#Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
public List<Location> findByplaceName(String placeName);
public List<Location> findByplaceDesc(String placeDesc);
}
html
<select>
<option>--select--</option>
<option th:each="p : ${unlist_places}"
th:text="${p.placeName}">
</option>
</select>
MySql Database
Can anyone please help me on how to send a nested json requestbody in postman for the below entity class.
ProcessRequestInfo.java
import java.sql.Date;
import java.time.ZonedDateTime;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="PROCESS_REQUEST_INFO")
public class ProcessRequestInfo {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String userName;
private String contactNumber;
#Embedded
private DefectiveComponentInfo defectiveComponentInfo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public DefectiveComponentInfo getDefectiveComponentInfo() {
return defectiveComponentInfo;
}
public void setDefectiveComponentInfo(DefectiveComponentInfo defectiveComponentInfo) {
this.defectiveComponentInfo = defectiveComponentInfo;
}
#Override
public String toString() {
return "ProcessRequestInfo [id=" + id + ", userName=" + userName + ", contactNumber=" + contactNumber
+ ", defectiveComponentInfo=" + defectiveComponentInfo + "]";
}
public ProcessRequestInfo(int id, String userName, String contactNumber,
DefectiveComponentInfo defectiveComponentInfo, ZonedDateTime date) {
super();
this.id = id;
this.userName = userName;
this.contactNumber = contactNumber;
this.defectiveComponentInfo = defectiveComponentInfo;
}
public ProcessRequestInfo() {
super();
// TODO Auto-generated constructor stub
}
}
DefectiveComponentInfo.java
import javax.persistence.Embeddable;
#Embeddable
public class DefectiveComponentInfo {
private String componentType;
private String componentName;
private Long quantity;
private String description;
public String getComponentType() {
return componentType;
}
public void setComponentType(String componentType) {
this.componentType = componentType;
}
public String getComponentName() {
return componentName;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
}
public Long getQuantity() {
return quantity;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return "DefectiveComponentDetail [componentType=" + componentType + ", componentName=" + componentName
+ ", quantity=" + quantity + ", description=" + description + "]";
}
public DefectiveComponentInfo(String componentType, String componentName, Long quantity, String description) {
super();
this.componentType = componentType;
this.componentName = componentName;
this.quantity = quantity;
this.description = description;
}
public DefectiveComponentInfo() {
super();
// TODO Auto-generated constructor stub
}
}
I have tried few nested json requestbody in postman but the inner class data is getting null.
Below is my json request body which I have tried.
{
"userName" : "sam",
"contactNumber" : "96014587555",
"DefectiveComponentInfo":
[ {
"componentType":"Integral",
"componentName":"Bummper",
"quantity":5,
"description": "Repair product"
}]
}
Thanks in advance!!!
You are almost there. few things to be corrected
{
"userName" : "sam",
"contactNumber" : "96014587555",
"defectiveComponentInfo": {
"componentType":"Integral",
"componentName":"Bummper",
"quantity":5,
"description": "Repair product"
}
}
What i have corrected here
As per #Ausgefuchster already mentioned about the incorrect usage of [] array syntax. Removed it.
Your property name is defectiveComponentInfo not capital D.
Try this:
"DefectiveComponentInfo":
{
"componentType": "Integral",
"componentName": "Bummper",
"quantity": 5,
"description": "Repair product"
}
Because you're using [] but these indicate a list.
How do I programmatically set a partition-key to every instance of a class that I send?
For instance, If I send a Personinstance, then I want to set the partition-key to be person.getId().
class Person {
String id;
public Person(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
}
#EnableBinding(Source.class)
public class SpringCloudStreamKinesisProducerApplication {
#InboundChannelAdapter(Source.OUTPUT)
public Person source() {
return new Person("my-id-123");
}
}
You can use the following property to set the partition key:
spring.cloud.stream.bindings.output.producer.partitionKeyExpression=payload.id
That will ensure that any Person instance that you are producing will be sent to the corresponding partition on the destination.
I have some problem with my Spring Boot application.
When I save one document in my repository application works fine, but when I save more than one documents, my aplication not working. Here is my code.
Article.java
import org.springframework.data.annotation.Id;
public class Article {
#Id
private String id;
private String title;
private String description;
public Article() {}
public Article(String title, String description) {
this.title = title;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return "Article{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", description='" + description + '\'' +
'}';
}
}
ArticleRepository.java
public interface ArticleRepository extends MongoRepository<Article, String> {}
DatabaseLoader.java
import java.util.HashSet;
import java.util.Set;
#Component
public class DatabaseLoader implements CommandLineRunner {
#Autowired
private ArticleRepository articleRepository;
#Override
public void run(String... args) throws Exception {
articleRepository.deleteAll();
// example 1: if I save one document to my repo everything works fine
// example: articleRepository.save(new Article("Title 1", "description 1"));
// console returns: Article{id='596f480e4e118123574a13f1', title='Title 1', description='description 1'}
// but here is problem
Set<Article> articleList = new HashSet<Article>(){
{
add(new Article("Title 1", "description 1"));
add(new Article("Title 2", "description 2"));
}
};
// because if I try to save my collection
articleRepository.save(articleList);
// console returns: com.mongodb.DuplicateKeyException:
// Write failed with error code 11000 and error message
// 'insertDocument :: caused by :: 11000 E11000 duplicate key
// error index: test.article.$user dup key: { : null }'
System.out.println(articleRepository.findAll());
}
}
Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(SimpleMongoApplication.class, args);
}
}
Do you know, why I have this problem?
Thank you for all your answers.
OK, here is #Afridi's solution.
All you need to do is adding #Document(collection="collection-name") annotation in your document. here is an example:
#Document(collection = "articles") // here is solution
public class Article {
#Id
private String id;
private String title;
private String description;
//getters, setters and constructor
}
I am trying to fetch a row in mongo db collection which has an array of object id which is the ObjectID of rows of data in other collection. I want to fetch the populated array of rows of data from ther collection for each record of data from the current collection.
My collections are like this
Jobs
- ObjectID
- [ObjectIds(Object Ids of candidates)]
Candidates
-ObjectID
-Name
-Age
I am using
import org.springframework.data.mongodb.core.mapping.DBRef;
in my model class to indicate the reference.
#Document(collection = "jobcreations")
public class Job {
#Id
private String id;
#DBRef
private List<Candidate> _candidates;
private String jobID;
public String getjobID() {
return jobID;
}
public void setjobID(String jobid) {
this.jobID = jobid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public JobCreation() {
super();
}
public List<Candidate> get_candidates() {
return _candidates;
}
public void set_candidates(List<Candidate> _candidates) {
this._candidates = _candidates;
}
public JobCreation(String jobID) {
super();
this.jobID = jobID;
}
#Override
public String toString() {
return "JobCreation [id=" + id + ", _candidates=" + _candidates + ", jobID=" + jobID + "]";
}
}
My candidate model class is like this
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "candidate")
public class Candidate {
#Id
private String _id;
private String name;
private int age;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the age
*/
public String getAge() {
return age;
}
/**
* #param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
public Candidate() {
super();
// TODO Auto-generated constructor stub
}
}
My main method where i call the db is
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
List<JobCreation> listJobCreation = mongoOperation.findAll(JobCreation.class);
for(JobCreation jc : listJobCreation){
System.out.println(jc.toString());
}
Here i am getting the error
Exception in thread "main" org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type com.mkyong.model.CandidateProfile
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:172)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:154)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:673)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:751)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1006)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$100(MappingMongoConverter.java:75)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:957)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:713)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$2.doWithAssociation(MappingMongoConverter.java:257)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:252)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:254)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:212)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:176)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:172)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:75)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1840)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1536)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1057)
at com.mkyong.core.App.main(App.java:29)
Kindly help me how to solve this error.
Thanks in advance