Apply the Exception handling in RESTful Java Web Service

Add the following method in the controller,


@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Book with this id not present")
@ExceptionHandler({ Exception.class })
public void handleException() {

}
Advertisements

persistence.xml – mysql

<?xml version=”1.0″ encoding=”UTF-8″?>
<persistence xmlns=”http://xmlns.jcp.org/xml/ns/persistence&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd&#8221;
version=”2.1″>
<persistence-unit name=”jpademo”>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<properties>
<property name=”hibernate.connection.url” value=”jdbc:mysql://localhost:3306/pilotdb”/>
<property name=”hibernate.dialect” value=”org.hibernate.dialect.MySQL5Dialect”/>
<property name=”hibernate.connection.driver_class” value=”com.mysql.jdbc.Driver”/>
<property name=”hibernate.connection.password” value=”admin”/>
<property name=”hibernate.connection.username” value=”root”/>
<property name=”hibernate.hbm2ddl.auto” value=”update”/>
<property name=”hibernate.show_sql” value=”true”/>
<property name=”hibernate.format_sql” value=”true”/>
</properties>
</persistence-unit>
</persistence>

BeanFactory vs ApplicationContext

BeanFactory

    • uses lazy initialization i.e. creates a singleton bean only when it is requested from it
    • used to be explicitly provided a resource object
ClassPathResource resource = new ClassPathResource("myBeans.xml");
BeanFactory context = new XmlBeanFactory(resource);
  • does not support internationalization
  • does not support Annotation based dependency Injection

ApplicationContext

  • uses eager initialization i.e. creates all singleton beans at the time of its own initialization
  • creates and manages resources objects on its own
  • supports internationalization
  • supports using annotation Annotation based dependency Injection @PreDestroy, @Autowired

persistence.xml – postgresql

<?xml version=”1.0″ encoding=”UTF-8″?>
<persistence xmlns=”http://java.sun.com/xml/ns/persistence&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&#8221;
version=”2.0″>
<persistence-unit name=”JPA-PU”
transaction-type=”RESOURCE_LOCAL”>
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
<property name=”javax.persistence.jdbc.driver”
value=”org.postgresql.Driver” /> <!– DB Driver –>
<property name=”javax.persistence.jdbc.url”
value=”jdbc:postgresql://localhost/siddharthpandey” /> <!– BD Mane –>
<property name=”javax.persistence.jdbc.user”
value=”postgres” /> <!– DB User –>
<property name=”javax.persistence.jdbc.password”
value=”password” /> <!– DB Password –>

<property name=”hibernate.dialect”
value=”org.hibernate.dialect.PostgreSQLDialect ” /> <!– DB Dialect –>
<property name=”hibernate.hbm2ddl.auto” value=”update” /> <!– create / create-drop / update –>

<property name=”hibernate.show_sql” value=”true” /> <!– Show SQL in console –>
<property name=”hibernate.format_sql” value=”true” /> <!– Show SQL formatted –>
</properties>

</persistence-unit>

</persistence>

 

6 – Creating sequence in hibernate

create sequence seq_purchase_id
increment by 1
start with 1;
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
	@SequenceGenerator(name="seq",sequenceName="seq_purchase_id",
				allocationSize=1)
private Integer purchaseId;

5 – Using TypedQuery

/* select all students using TypedQuery */
TypedQuery query = em.createQuery("SELECT c FROM Student c", Student.class);
List studentList = query.getResultList();
for (Student s : studentList) {
	System.out.println(s);
}

4 – Using Query

/* select all students using Query */
Query query = em.createQuery("SELECT e FROM Student e");
List studentList = query.getResultList();
for (Student s : studentList) {
			System.out.println(s);
}