Wednesday, 16 November 2016

Getting started Jersey.

1. Project build and management configuration is described in the pom.xml located in the project root
directory.
REF : pom elements
2. Project sources are located under src/main/java.
3. Project resources are located under src/main/resources.
4. Project web application files are located under src/main/webapp.

5. To compile and package the application into a WAR, invoke the following maven command in your
console:

ex : mvn clean package

logs :

Packaging webapp
[INFO] Assembling webapp [simple-service-webapp] in [.../simple-service-webapp/target/[INFO] Processing war project
[INFO] Building war: .../simple-service-webapp/target/simple-service-webapp.war

Now you are ready to take the packaged WAR (located under ./target/simple-servicewebapp.
war) and deploy it to a Servlet container of your choice.

Jersey dependency maven :

1) jersey-server, jersey-json, jersey-multipart.

Spring dependency :

spring-core,context,context-support,web,aop.

spring security.

security-web,security-config,security-taglib,security-core,security-acl

spring+jersey

spring,core,web,beans,context,asm,aop.

jackson:

jackson-jaxrs-json-provider


apache commons :

utility for string manipulation

connection pool :

dbcp

Apache-commns :

Apache Commons Codec provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.

Hibernate :

hibernate-core

spring-transaction:

REF : transaction   why we go for spring transaction

hibernate entity manager
table mapping

spring-orm



6. Root Resource Classes
Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path

7. @Produces
annotation is used to specify the MIME media types of representations a resource can produce and send back to the
client.
Using multiple output MIME types
@Produces({"application/xml", "application/json"})


@Consumes
annotation is used to specify the MIME media types of representations that can be consumed by a resource.

@QueryParam
is used to extract query parameters from the Query component of the request URL.

@DefaultValue
annotation, will be assigned to the step method parameter. If the
"step" value cannot be parsed as a 32 bit signed integer then a HTTP 404
ex :
 public Response smooth(
4 @DefaultValue("2") @QueryParam("step") int step

@FormParam

it extracts information from a request representation that is of the MIME media type
"application/x-www-form-urlencoded"

@Context HttpHeaders headers,

@Singleton
annotation says that the resource
will be managed as singleton and not in request scope. The sub-resource locator method returns a class which means that the runtime will managed the resource instance and its life-cycle. If the method would return instance instead, the Singleton annotation would have no effect and the returned instance would be used.


Securiry -- done
Multipart  -- progress -- 8.3
Filters -- 9.0







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() {

}

}

Saturday, 27 August 2016

Java 8 JDBC

Java 8 : The JDBC-ODBC Bridge has been removed.

There is plenty of option to try. refer the Stackoverflow for futher references. 


Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

Java 1.8 Concurrency feature "ConcurrentHashMap as a Set"

The feature will return set of key in the map as Set.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "Networking");
map.put("Microsoft", "Windows10");
map.put("facebook", "Social");
map.put("Google", "SearchEngine");

KeySetView<String,String> set = map.keySet();
System.out.println(set);
}

}

Result : [Google, facebook, Microsoft, Lnkedin] .

Image :


ref :
Java API 


Behaviour of ConcurrentHashMap when it has Duplicate key.

Behaviour of ConcurrentHashMap when it has Duplicate key.

ConcurrentHashMap will overwrite previous value for a given key in Map. The object whos reference is lost are eligible for garage collection.

public class VoiceConcurrentHashMap {

public static void main(String[] args) {

ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String, String>();
System.out.println(map);
map.put("Lnkedin", "profile");
map.put("Lnkedin", "profile");
map.put("Lnkedin", "jagdeesh");

}
}

ref :
1. Java API
2. Stackoverflow


Wednesday, 24 August 2016

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

When supplying json object from postman. if Class contain a list object. Provide it as a array [].
ex :
{
    "name" : "raman",
    "employeeId" : 12345,
"orderList" : {"orderId" : 1,"orderName" : "rest json book"}
}
solution :
{
"name" : "raman",
"employeeId" : 12345,
"orderList" : [{"orderId" : 1,"orderName" : "rest json book"}]
}