Spring Boot Lombok API not serializing - spring-boot

I have a simple Spring Boot controller with a simple Object, which is annotated with Lombok, when I tried to post data to the controller the object to not serializing.
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
#EqualsAndHashCode(callSuper = true)
#ToString(callSuper = true)
public class Employee extends BaseDomain {
private String firstName;
private String middleName;
private String lastName;
private String email;
private String mobileNo;
#PostMapping
public Employee saveEmployee(Employee employee) {
log.debug("Employee save {}", employee);
return employeeService.saveOrUpdateEmployee(employee);
}
}

#PostMapping
public Employee saveEmployee(#Requestbody Employee employee) {
log.debug("Employee save {}", employee);
return employeeService.saveOrUpdateEmployee(employee);
}
#Requestbody is missing

Related

Mongodb created date return null

When I create product createdDate return correct, but on update createdDate return null, I tried to implements persistable but still not working
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#SuperBuilder
public class BaseEntity implements Persistable<String> {
#MongoId(value = FieldType.OBJECT_ID)
private String id;
#CreatedDate
private Date createdDate;
#LastModifiedDate
private Date updatedDate;
#Override
public boolean isNew() {
return StringUtils.isEmpty(id);
}
}
To get value for createdDate you need to add #EnableMongoAuditing to main spring class
#SpringBootApplication
#EnableMongoAuditing

Why the get request give empty response in Spring Boot?

I'm trying to make simple rest services which can save the data to h2 database using JPA and show the data in response, but when I try POST request, the data that saved is null even though when I check the h2 console, the ID is entered saved because it use #GeneratedValue, but other is null. also when I want try GET request, the response give me null json
#Entity
public class MS_Product {
#GeneratedValue
#Id
#Getter
private long productId;
#Getter #Setter
private String productName;
#Getter #Setter
private int productPrice;
#Getter #Setter
private int productStock;
#UpdateTimestamp
#Getter
private LocalDateTime updatedDate;
protected MS_Product() {
}
public MS_Product(long productId, String productName, int productPrice, int productStock, LocalDateTime updatedDate) {
super();
this.productId = productId;
this.productName = productName;
this.productPrice = productPrice;
this.productStock = productStock;
this.updatedDate = updatedDate;
}
}
public interface MS_ProductRepository extends JpaRepository<MS_Product, Long>{
}
#RestController
public class MS_ProductController {
#Autowired
MS_ProductRepository productRepository;
#GetMapping("/products")
public ResponseEntity<MS_Product> findAllProduct(){
try {
List<MS_Product> products = productRepository.findAll();
return new ResponseEntity(products, HttpStatus.OK);
}catch(Exception e){
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
#PostMapping("/products")
public ResponseEntity<MS_Product> createProduct(#RequestBody MS_Product product){
try {
MS_Product savedProduct = productRepository.save(product);
return new ResponseEntity(product, HttpStatus.CREATED);
}catch(Exception e){
return new ResponseEntity(null, HttpStatus.EXPECTATION_FAILED);
}
}
}
Try
#Entity(name="your_table_name")
public class Student {
By design, the in-memory database is volatile and data will be lost when we restart the application.
We can change that behavior by using file-based storage. To do this we need to update the spring.datasource.url:
spring.datasource.url=jdbc:h2:file:/data/demo
Ref: https://www.baeldung.com/spring-boot-h2-database
You need
#Column(name = "productId")
on every your field which you need to map to table column

Spring boot MongoDb complex query

I have been learning myself MongoDB implementation in Spring Boot.
However, I came into a problem with complex queries.
I cannot find any right solution for how to implement complex queries to MongoDB from Spring boot.
I am querying the database with MongoRepository interface implementation.
Let's say that I have three collections:
Person - 1 Person can have many Pets.
Pet - 1 Pet can have 1 PetToy and 1 Person who owns him.
PetToy - 1 PetToy can belong to 1 Pet.
POJO classes are bellow
What do I want to achieve?
I want to make a query, which would be returned me a Person, whose Pet has a Toy (PetToy) with the name "Teddy".
I could not have found a way how to do it. Furthermore, is it the best practice to even use such complex queries, or is it better to write more of little ones in MongoDB?
POJOs:
#Document
#Data
#ToString
public class Person {
#Id
private String id;
private String firstname;
private String lastname;
private int age;
#DBRef
private Pet pet;
}
#Document
#Data
#ToString
public class Pet {
#Id
private String id;
private String name;
private int age;
#DBRef
private List<PetToy> toys;
}
#Document
#Data
#ToString
public class PetToy {
#Id
private String id;
private String name;
}
I have tried to use MongoRepositories; however, I was not able to make the complex query.
How can one write such a query to a MongoDB from Spring Boot?
Thank you very much in advance.
If you can use embedded attributes, the class model should be:
#Document
#Data
#Builder
public class Person {
#Id
private String id;
private String firstName;
private String lastName;
private int age;
private List<Pet> pets;
}
#Data
#Builder
public class Pet {
private String name;
private int age;
private List<PetToy> toys;
}
#Data
#Builder
public class PetToy {
private String name;
}
The repository with the method that achieves what you want:
public interface PersonRepository extends MongoRepository<Person, String> {
List<Person> getByPetsToysName(String name);
}
The getByPetsToysName method basically navigate between Person's attributes Person->pets->toys->name. More info here.
An example
#Configuration
#EnableMongoRepositories
public class TestMongo implements CommandLineRunner {
private final PersonRepository repository;
public TestMongo(PersonRepository repository) {
this.repository = repository;
}
#Override
public void run(String... args) throws Exception {
repository.save(Person.builder()
.firstName("John")
.lastName("Doe")
.age(20)
.pets(Stream.of(Pet.builder()
.name("Ursa")
.age(1)
.toys(Stream.of(PetToy.builder()
.name("Teddy")
.build())
.collect(Collectors.toList()))
.build())
.collect(Collectors.toList()))
.build());
repository.save(Person.builder()
.firstName("Phillip")
.lastName("Larson")
.age(21)
.pets(Stream.of(Pet.builder()
.name("Bella")
.age(5)
.toys(Stream.of(PetToy.builder()
.name("Lolo")
.build())
.collect(Collectors.toList()))
.build())
.collect(Collectors.toList()))
.build());
List<Person> persons = repository.getByPetsToysName("Teddy");
System.out.println(persons.size());
List<Person> persons1 = repository.getByPetsToysName("Lolo");
System.out.println(persons1.size());
}
}
Logs:
find using query: { "pets.toys.name" : "Teddy" } fields: Document{{}} for class: class Person in collection: person
If you want more complex queries you can to take a look at the Spring Data MongoDB docs.

Spring 4.1.7 validate request body

I know this issue has been around there in other post, but after applying the fiux suggested was not working.
I am using spring 4.1.7 version, i want to validate the RequestBody from post rest call. For doing this i tried following set of codes, but it was not working as i expected.
My Request body pojo classes.
ParentPojo.class
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Builder
#ToString
#Validated
public class ParentPojo<T> implements Serializable{
#NotNull
private String cNumber;
#NotNull
private String statusCode;
#NotNull T child;
}
ChildPojo.class
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Builder
#ToString
#Validated
public class ChildPojo{
#NotNull
private String name;
#NotNull
private String address;
#NotNull
private String pin;
}
Controller:
Adding only methods
#Autowired
#Qualifier("validator")
private Validator validator;
#InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
}
#RequestMapping(produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
#ResponseStatus(HttpStatus.CREATED)
public Messageresponse<ChildPojo> create(#NotNull(groups = {ParentPojo.class, ChildPojo.class})
#Valid #Validated({ParentPojo.class, ChildPojo.class}) #RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
System.out.println("new version 8="+bindingResult.hasErrors());
validator.validate(ParentPojo, bindingResult);
if(bindingResult.hasErrors()) {
System.out.println("Non formated form stuff.");
}
return service.create(ParentPojo);
}
#RequestMapping(value = "/{create}", produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
#ResponseStatus(HttpStatus.CREATED)
public Messageresponse<ChildPojo> create1(#NotNull #Valid #RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
System.out.println("new version 8="+bindingResult.hasErrors());
validator.validate(ParentPojo, bindingResult);
if(bindingResult.hasErrors()) {
System.out.println("Non formated form stuff.");
}
return service.create(ParentPojo);
}
application context xml:
<mvc:annotation-driven validator="validator">
</mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
jar file tried:
hibernate-validator-4.3.0.Final
hibernate-validator-4.1.0.Final
hibernate-validator-4.0.2.GA
validation-api-1.1.0.Final
spring-context-4.1.7.RELEASE
But nothing was working with all the above combination for the request below:
in below request "pin" is missed and the controller method #Valid #RequestBody expected to handle this request as Bad Request. instead it is accepting the request body and processing further.
{
"cNumber" : "ff",
"statusCode" : "ddd",
"child" : {
"name" : "ll",
"address" : "ll"
}
Look at this question Here
You need to decorate the child pojo as #Valid
public class ParentPojo<T> implements Serializable{
#NotNull
private String cNumber;
#NotNull
private String statusCode;
#Valid
#NotNull
T child;
}

How I can get one column value from a table?

I use Spring boot and Spring Data.
I have a contact entity with the id and firstName columns.
#Entity
#Table(name = "Contact")
public class Contact {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String firstName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
I use repository with Spring Data to find my data.
public interface contactRepository extends CrudRepository<Contact, Long> {
}
My controller, example with getAll:
#RequestMapping(value = "/getAllContact", produces = "application/json")
public List<Contact> getAllClients(){
return repo.getAll();
}
My controller works but I don't know how to return all values in column firstName in my controller. I tried with a query, It works but it only returns a list of values and not the json:
#Query(value = "SELECT firstName FROM Contact" )
List<Contact> findAllFirstName();
Example:
["Pierre", "Jean"]
And i want this (in Json):
[{"firstName ": "Pierre" },{"firstName ":"Jean"}]
How do I do this?
Use the projection and excerpt support in Spring Data Rest. Whilst adding in JsonIgnore annotations does work, it's inflexible as you can only ignore at compile time not run time.
See
http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
Make sure jackson libraries are in your classpath. Then add #ResponseBody in your controller method to return json output. Also add #JsonIgnore in id in your entity to exclude it from json output.
#RequestMapping(value = "/getAllContact", produces = "application/json")
#ResponseBody
public List<Contact> getAllClients(){
return repo.getAll();
}
#Entity
#Table(name = "Contact")
public class Contact {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
#JsonIgnore
private int id;
private String firstName;
.....
}

Resources