Validating response from REST API call - java

I am working on error handling a response mapping. Before I go ahead and map the response to my domain objects, I want to validate the response. Check for errors.
I am planning to have a Validator.java class and implement validation methods for each of the API call.
Is there any alternative way in spring to do this?

package com.people.net;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
public class UserInfo {
//Unicode check
#Pattern(regexp="[0-9a-zA-Z\\s-]+", message="chars,numbers allowed only")
String name;
int id;
#Pattern(regexp="([0-9]{10})", message="minLength=maxLength=10 only numbers")
String pin;
#Email
String email;
#Size(max=5, message="5 chars max")
String emailType;
#Size(max=5, message="5 chars max")
String addressType;
#Size(max=300, message="5 chars max")
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UserInfo(String name, String pin, String id) {
super();
this.id = Integer.parseInt(id);
this.name = name;
this.pin = pin;
}
public UserInfo(int id,String name, String pin, String email, String emailType, String addressType, String address) {
super();
this.id = id;
this.name = name;
this.pin = pin;
this.email = email;
this.emailType = emailType;
this.addressType = addressType;
this.address = address;
}
public UserInfo() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmailType() {
return emailType;
}
public void setEmailType(String emailType) {
this.emailType = emailType;
}
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

You should explore hibernate validator which is the reference implementation JSR-349 for bean validation. more info is here https://jcp.org/en/jsr/detail?id=349 .
Also hibernate validator is a industry standard for doing the bean validation(lots of stuff available on internet) and comes with lots of popular framework like dropwizard etc.it provide annotation based validation and allow you to write your own custom annotation based validation.
please refer on step by step guide http://www.journaldev.com/2668/spring-validation-example-mvc-validator, where author uses it with the spring.

Related

Mapping Json to POJO without Jackson and only JDK classes

Recently at a test, I came across a problem to do some processing after querying the response data from a restful service. I had to write a web service consumer and do some processing using Java.
While I was able to consume the service using Http classes from jdk, I didn't knew of any way to map the response json in their respective pojo's, without using Jackson's or other external mapper libraries.
Now I have been trying to do that. Until now I have tried to change the incoming Json to byte array and deserialize and map into the pojo, but that didn't worked. I remember with JAX-B unmarshalling was possible but it has been carved out of jdk after java 8, and I have been working with higher version JDK 11.
I also tried getting the response as streams but then data processing does not remains equally straightforward, as it would have been in case of working with model classes. I am in split, any way out of it would be very appreciable ..
HttpRequest req = HttpRequest
.newBuilder(new URI("https://jsonmock.hackerrank.com/api/transactions/search?userId=4"))
.GET().build();
//HttpResponse<byte[]> response = HttpClient.newHttpClient().send(r, BodyHandlers.ofByteArray());
HttpResponse<Stream<String>> response = HttpClient.newHttpClient().send(req, BodyHandlers.ofLines());
Stream<String> responseStream = response.body();
Natively you can do it using Regex and plenty complex custom logic assuming you handling the JSON as string changing the response Line as below.
HttpResponse response = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
I assume that the models you could use are below
Location Model
`
public class Location {
private int id;
private String address;
private String city;
private int zipCode;
public Location(int id, String address, String city, int zipCode) {
this.id = id;
this.address = address;
this.city = city;
this.zipCode = zipCode;
}
public Location() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getZipCode() {
return zipCode;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
}
`
User Model
`
public class User {
private int id;
private int userId;
private String userName;
private double timestamp;
private String txnType;
private String amount;
private Location location;
private String ip;
public User(int id, int userId, String userName, double timestamp, String txnType, String amount, Location location, String ip) {
this.id = id;
this.userId = userId;
this.userName = userName;
this.timestamp = timestamp;
this.txnType = txnType;
this.amount = amount;
this.location = location;
this.ip = ip;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getTimestamp() {
return timestamp;
}
public void setTimestamp(double timestamp) {
this.timestamp = timestamp;
}
public String getTxnType() {
return txnType;
}
public void setTxnType(String txnType) {
this.txnType = txnType;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
`
Generally Java has not any build in parser. You may find this below
Java Built-in data parser for JSON or XML or else
The most simple library you might find is described here
https://www.oracle.com/technical-resources/articles/java/json.html
and you might find it as maven dependency below
https://mvnrepository.com/artifact/org.json/json
To do that you would have to write your own Json parser that would do the same as Jackson, Gson Simple Json or any other external Json library. In other words you would be re-inventing the wheel. While it might be very interesting it would be pretty useless. That is why those libraries have been written in the first place.

How to solve "Mapper method has an unsupported return type"

I'm getting the following error:
Mapper method 'nutri.api.infrastructure.datasource.client.ClientMapper.saveClient' has an unsupported return type: class nutri.api.domain.model.Client
I'm using Java 15, PostgreSQL, and MyBatis for my project.
I only understand that something is wrong with my Client.java and ClinetMapper.java but not really sure what's causing this error. Any suggestion would help a lot.
Here is my code
Client.java
import java.util.UUID;
public class Client {
int id;
String clientNumber;
String name;
String email;
String healthCondition;
public Client(int id, String clientNumber, String name, String email, String healthCondition) {
this.id = id;
this.clientNumber = UUID.randomUUID().toString();
this.name = name;
this.email = email;
this.healthCondition = healthCondition;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClientNumber() {
return clientNumber;
}
public void setClientNumber(String clientNumber) {
this.clientNumber = clientNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHealthCondition() {
return healthCondition;
}
public void setHealthCondition(String healthCondition) {
this.healthCondition = healthCondition;
}
}
ClientMapper.java
#Options(useGeneratedKeys=true, keyProperty="id") Client saveClient(Client client);
ClientMapper.xml(MyBatis file)
<mapper namespace="nutri.api.infrastructure.datasource.client.ClientMapper">
<insert id="saveClient" parameterType="nutri.api.domain.model.Client">
INSERT INTO client.data (
id,
client_number,
name,
email,
health_condition
) VALUES (
nextval('client.client_id_seq'),
#{clientNumber},
#{name},
#{email},
#{healthCondition}
)
</insert>
For insert statement, the method return type must be void or int.
If you use int, the number of inserted rows (always 1 in your case) will be returned.
The generated key will be set to the id field of the parameter instance.
Also, you should specify keyColumn.
#Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
int saveClient(Client client);
Client param = ... // prepare parameter
int count = clientMapper.saveClient(param);
// param.getId() will return the generated key

Create list with unique id in Java and manipulate it

I want to create a programm that creates a list that looks like this
ID: 1
Name: Example
Surname: Example
email: example
//New list
ID: 2
Name: Example
Surname: Example
email: example
and then when i want to change something (like Name: ) i'd like to change it by id, so it can only be changed inside the list with ID: 2
You should use a HashMap.
Create a class (let's class it YourClass) that contains ID, name, surname and email instance variables.
Then create a HashMap where the key is the identifier and the value is YourClass:
Map<Integer,YourClass> map = new HashMap<>();
map.put(objectOfYourClassWithID1.getID(),objectOfYourClassWithID1);
map.put(objectOfYourClassWithID2.getID(),objectOfYourClassWithID2);
if (map.containsKey(2)) {
map.get(2).setSomeProperty(newValue); // this will only change the object whose ID is 2
}
you can create class like this
public class Record{
private int id;
private String name;
private String Surname;
private String email;
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;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
and then use it like this:
Record record1 = new Record();
record1.setId(1);
record1.setName("example");
record1.setSurname("example");
record1.setEmail("example");
Record record2 = new Record();
record2.setId(2);
record2.setName("example");
record2.setSurname("example");
record2.setEmail("example");
Map<Integer,Record> recordMap = new HashMap<Integer, Record>();
recordMap.put(record1.getId(),record1);
recordMap.put(record2.getId(),record2);]
recordMap.get(2).getName();//example
recordMap.get(2).setName("ebi");
recordMap.get(2).getName();//ebi
import java.util.ArrayList;
import java.util.List;
public class Person {
private int id;
private String name;
private String Surname;
private String email;
public Person(int id, String name, String surname, String email) {
super();
this.id = id;
this.name = name;
Surname = surname;
this.email = email;
}
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;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person(1, "example", "example", "example"));
list.add(new Person(2, "example", "example", "example"));
}
}

Spring MVC form validation does't work for nested complex types

I am implementing a sample Spring MVC Form with Form Validation. I have a complex type Address as bean property for Student form bean. And I have added form validation #NotEmpty for Address bean properties. But the same is not reflecting in the UI. But form validation works for other primitive types of Student form bean.
So, Validation works perfectly for Student form bean but not for nested complex types like Address within Student form bean.
I am trying understand the reason and a fix.
Spring version 4.0+.
Hibernate Validator api:5.2.4
Student POJO:
package com.xyz.form.beans;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import com.xyz.validators.DateNotEmpty;
import com.xyz.validators.ListNotEmpty;
public class Student {
#Size(min = 2, max = 30)
private String firstName;
#Size(min = 2, max = 30)
private String lastName;
#NotEmpty
private String gender;
#DateNotEmpty
#Past
private Date DOB;
private String email;
private String mobileNumber;
#ListNotEmpty
private List<String> courses;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getDOB() {
return DOB;
}
public void setDOB(Date dOB) {
DOB = dOB;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
}
Address POJO:
package com.xyz.form.beans;
import org.hibernate.validator.constraints.NotEmpty;
import com.xyz.validators.LongNotEmpty;
public class Address {
#NotEmpty
private String houseNo;
#NotEmpty
private String street;
#NotEmpty
private String area;
#NotEmpty
private String city;
#LongNotEmpty
private Long pin;
public String getHouseNo() {
return houseNo;
}
public void setHouseNo(String houseNo) {
this.houseNo = houseNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Long getPin() {
return pin;
}
public void setPin(Long pin) {
this.pin = pin;
}
}
Student Controller:
#RequestMapping(value = "/newStudentDetails.do", method = RequestMethod.POST)
public ModelAndView newStudentDetails(
#Valid #ModelAttribute("student") com.xyz.form.beans.Student studentFormBean,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return new ModelAndView("newStudentPage");
}
Student studentDto = new Student();
studentDto.setFirstName(studentFormBean.getFirstName());
studentDto.setLastName(studentFormBean.getLastName());
studentDto.setGender(studentFormBean.getGender());
studentDto.setDOB(new Date(studentFormBean.getDOB().getTime()));
studentDto.setEmail(studentFormBean.getEmail());
studentDto.setMobileNumber(studentFormBean.getMobileNumber());
StringBuilder sb = new StringBuilder();
sb.append(studentFormBean.getAddress().getHouseNo() + ", ");
sb.append(studentFormBean.getAddress().getStreet() + ", ");
sb.append(studentFormBean.getAddress().getArea() + ", ");
sb.append(studentFormBean.getAddress().getCity() + "-");
sb.append(studentFormBean.getAddress().getPin());
studentDto.setAddress(sb.toString());
studentDto.setCourses(studentFormBean.getCourses());
studentDao.createStudent(studentDto);
ModelAndView mav = new ModelAndView("newStudentSuccess");
return mav;
}
Thanks,
Viswanath
You need to annotate your complex types with #Valid.
This is the reference (which references here)
Hi lets try #ModelAttribute("student") #Valid com.xyz.form.beans.Student studentFormBean in place of #Valid #ModelAttribute("student")
For nested complex types, you have to activate the direct field access. Just like below:
#org.springframework.web.bind.annotation.ControllerAdvice
public class ControllerAdvice {
#InitBinder
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.initDirectFieldAccess();
}

It show the error that Customer is already defined.Please let me know whats wrong and how to correct it

There is a compliation error stating that class name is already define i can't find the way to resolve it
further the class name are declared only once and can't find the place where the things are going wrong
package practo;
import java.io.*;
import java.lang.*;
import java.util.*;
#SuppressWarnings("unused")
class Customer /* compilation error occurs here */
{
private int id;
private String name;
private String email;
private String address;
void setid(int id)
{
this.id=id;
}
int getid()
{
return id;
}
void setname(String name)
{
this.name=name;
}
String getname()
{
return name;
}
void setemail(String email)
{
this.email=email;
}
String getemail()
{
return email;
}
void setaddress(String address)
{
this.address=address;
}
String getaddress()
{
return address;
}
class PhoneNumber
{
private String phoneNumber;
private String heldFromDate;
private String heldToDate;
void setphoneNumber(String phoneNumber)
{
this.phoneNumber=phoneNumber;
}
String getphoneNumber()
{
return phoneNumber;
}
void setheldToDate(String heldToDate)
{
this.heldToDate=heldToDate;
}
String getheldToDate()
{
return heldToDate;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
class NumberType
{
private String code;
private String description;
void setcode(String code)
{
this.code=code;
}
void setdescription(String description)
{
this.description=description;
}
String getcode()
{
return code;
}
String getdescription()
{
return description;
}
}
}
}
class x1
{
public void main(String args[])
{
#SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
Customer c=new Customer();
Customer.PhoneNumber p=c.new PhoneNumber();
Customer.PhoneNumber.NumberType n=p.new NumberType();
System.out.println("Enter the customer details");
System.out.println("Enter the id :");
int id=s.nextInt();
c.setid(id);
System.out.println(c.getid());
System.out.println("Enter the name :");
String name=s.nextLine();
c.setname(name);
System.out.println(c.getname());
System.out.println("Enter the email :");
String email=s.nextLine();
c.setemail(email);
System.out.println(c.getemail());
System.out.println("Enter the address :");
String address=s.nextLine();
c.setaddress(address);
System.out.println(c.getaddress());
System.out.println("Enter the customer contact details");
System.out.println("Enter the phone number :");
String phoneNumber=s.nextLine();
p.setphoneNumber(phoneNumber);
System.out.println(p.getphoneNumber());
System.out.println("Enter the held from date (dd/MM/yyyy) :");
String heldFromDate=s.next();
p.setHeldFromDate(heldFromDate);
System.out.println(p.getHeldFromDate());
System.out.println("Enter the held to date (dd/MM/yyyy) :");
String heldToDate=s.next();
p.setheldToDate(heldToDate);
System.out.println(p.getheldToDate());
System.out.println("Enter number type code :");
String code=s.next();
n.setcode(code);
System.out.println(n.getcode());
System.out.println("Enter number type description");
String description=s.next();
n.setdescription(description);
System.out.println(n.getdescription());
}
}
Your class does not give me any compilation error. You might try making the class public i.e. public class Customer and file name having the name Customer.java. It may happen that the package practo already contains a class named Customer.
Can you please verify Customer class is not duplicate ? If it is not there, can you choose Clean from the Project menu, it might fix these errors.
Sometime eclipse trouble us.
Lots of recommendations for improvement:
Open public class per file, and a file for each class. Your arrangement is confusing.
Learn and follow the Java coding standards.
Using a Date for a date instead of a String is a better design, especially with JDK 8 and the java.time package.
Learn JUnit instead of that x1.main.
These are examples of how your classes should look.
Customer.java
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class Customer {
private int id;
private String name;
private String email;
private String address;
public Customer() {
this(0, "", "", "");
}
public Customer(int id, String name, String email, String address) {
this.id = id;
this.name = name;
this.email = email;
this.address = address;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
PhoneNumber.java:
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class PhoneNumber {
private String phoneNumber;
private String heldFromDate; // Bad design. This ought to be a Date, not a String
private String heldToDate; // Bad design. This ought to be a Date, not a String
public PhoneNumber() {
this("", "", "");
}
public PhoneNumber(String phoneNumber, String heldFromDate, String heldToDate) {
this.phoneNumber = phoneNumber;
this.heldFromDate = heldFromDate;
this.heldToDate = heldToDate;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
public String getHeldToDate() {
return heldToDate;
}
public void setHeldToDate(String heldToDate) {
this.heldToDate = heldToDate;
}
}
Check if you have another class called Customer in the package practo. That would cause a name conflict.

Categories

Resources