Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Wednesday, 16 November 2016

Stateless Authentication with Spring Security


1) for content and format ref :
http://blog.jdriven.com/2014/10/stateless-spring-security-part-2-stateless-authentication/

2) http://www.codingpedia.org/ama/how-to-secure-jersey-rest-services-with-spring-security-and-basic-authentication/
3) http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/


Sunday, 4 September 2016

Context initialization failed

Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private edu.scramjet.service.AppUserService edu.scramjet.controller.UserController.appUserService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.scramjet.service.AppUserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Solution :
If you look at stack. Spring container is unable to create bean. for userController. unable to autowire the appUserService.

ex :  public class void UserController {
 
       @Autowired
private AppUserService appUserService;     ///  interface object

      @RequestMapping(value="/create",method=RequestMethod.POST,
produces=MediaType.APPLICATION_JSON_VALUE,
consumes=MediaType.APPLICATION_JSON_VALUE)
public User createAppUser(@RequestBody User user ){
System.out.println(user);

appUserService.createUser();



return user;
}

   
     }

If AppUserService  is object  interface and its autowired. We need to provide Implementation class for bean creation.


public interface AppUserService {

public void createUser();
}

@Service
public interface AppUserService implements  AppUserServiceImpl {

public void createUser() {

}

}

Thursday, 7 July 2016

spring jersey intigration

steps 1 :

in web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>\WEB-INF\ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>jerseyService</servlet-name>
<servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.spring.container.servlet.SpringServlet</param-name>
<param-value>com.jersey.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyService</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>

</web-app>

step 2: application context in web-inf folder.
include namepace for spring module.
ref : http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/xsd-config.html

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd " >

<context:component-scan base-package="com.jersey.api" />

 
</beans>

step 3: configure pom with spring,spring-jersey depencdency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myjersey</groupId>
  <artifactId>Onejersey</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Onejersey Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
  <jersey.spring>1.9</jersey.spring>
  </properties>
 




  <dependencies>


  <!-- spring  -->
 
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>4.1.3.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>4.1.3.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>4.1.3.RELEASE</version>
</dependency>


 

  <!-- spring  -->


  <!-- jersey  -->
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.9</version>
</dependency>
<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-json</artifactId>
   <version>1.9</version>
</dependency>
<dependency>
   <groupId>com.sun.jersey.contribs</groupId>
   <artifactId>jersey-multipart</artifactId>
   <version>1.9</version>
</dependency>


  <!-- jersey -->
 
    <!-- spring-jersey  -->
  <dependency>
   <groupId>com.sun.jersey.contribs</groupId>
   <artifactId>jersey-spring</artifactId>
   <version>${jersey.spring}</version>
   <exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
   
   </exclusions>
</dependency>

<!-- jackson -jaxrs  -->
<dependency>
   <groupId>com.fasterxml.jackson.jaxrs</groupId>
   <artifactId>jackson-jaxrs-json-provider</artifactId>
   <version>2.5.5</version>
</dependency>
<!-- jackson -jaxrs  -->


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>Onejersey</finalName>
  </build>
</project>






step 4 : java application



/**
 *
 */
package com.jersey.api;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.springframework.stereotype.Service;

/**
 * @author voice
 *
 */
@Service
@Path("/hello")
public class Employee {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response helloWorld(){
ResponseBuilder rs = Response.status(Status.OK);
System.out.println("HI this is jagdeesh");
return rs.build();
}

}





ref :
spring-jersey
spring module

Monday, 27 June 2016

java.lang.IllegalStateException: LifecycleProcessor not initialized

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date


check the configuration in
1. applicationcontext.xml
2.pom.xml

observation :
1. make sure to upgrade dependency to 4.0 release or 3.0 release.




Wednesday, 8 June 2016

not-null property references a null or transient value

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : entity.VoiceProfile.email; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : entity.voiceProfile.email

 conclustion :
 While persisting into the DB get the users VoicePofile by id or check if any user exist for the voiceprofile or not.
 if exist get and then store in db.

Monday, 6 June 2016

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException

SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries

Ex :
error :
{
"name" : "voice"
"email" : "voice@gmail.com"

}
corrected :
{
"name" : "voice" ,
"email" : "voice@gmail.com"

}

Solution :  In the corrected solution put a comma.

Tuesday, 24 May 2016

WARN (SqlExceptionHelper.java:233) - Unknown column 'xxx' in 'field list'

 WARN  (SqlExceptionHelper.java:233) - Unknown column 'EditedOn' in 'field list'

Solution :
1) In hibernate check the entity class.
2) In table check the column names.
3) Compare both the entity and column name.
4.Caused due to spelling mistake.

Friday, 20 May 2016

what is purpose of calling chain.doFilter() in servlet


what is purpose of calling chain.doFilter() in servlet
chain.doFilter(request, response);

The name chain suggests that you have a sequence of filters, with each filter doing some processing and then passing on to the next in sequence, so each object has a chain member to point to the next filter in the sequence, which gets called after the filter has performed its own processing. The last in the sequence will then probably have null as the chain value, or it knows on its own that it is the last one in the sequence.

Ref : stackoverflow
author JK

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
...

chain.doFilter(request, response);
}

after executing the method  on calling chain.doFilter() it will call filter present in web.xml in the order of defination.


<filter>
  <filter-name>VoiceLoginFilter</filter-name>
  <filter-class>com.voice.filters.VoiceLoginFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>VoiceLoginFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Thursday, 19 May 2016

When to use detached criteria and criteria.

When to use detached criteria and criteria.

detached criteria : if we want to create a query wihout any session
Ref : Stackoverflow

another alternative is

If you are using Spring and choose to use HibernateTemplate, it doesn't provide createCriteria() method.

Ref :  spring

Thursday, 12 May 2016

Write operations are not allowed in read-only mode (FlushMode.MANUAL)


org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
Below of the piece of code worked for me. Added @Transactional annotation where i was performing insert operation
@Transactional
public void sendVoice(VoiceUser voiceUser) {
                    feedSendVoice.save(voiceUser)
 }

Thursday, 7 April 2016

Spring logging

error :

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component

include the common-logging dependency.

Commons Logging is the logging framework that Spring uses to log


Spring

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

1) configure the required spring dependency to pom.xml

or

2) configure the spring  maven dependency library to java build path.

ex :  go to properties->deployment assembly->add ->java build path 

Wednesday, 16 March 2016


Error :
Cannot find class [] for bean with name defined in ServletContext resource [/WEB-INF/spring/applicationContext.xml]

Hi this is the common error when working with maven peoject.

solution :

step 1. go to command prompt and do maven clean update ex : mvn clean install step 2 : reload the project in eclipse