Eclipse IDE

Upgrading/Changing Dynamic Web Module Version in Maven Spring Application.

For this purpose I am just referencing the same sample application which we created in my last post i.e. “CREATING RESTFUL WEB SERVICES IN JAVA USING SPRING 4.2.5”.We will upgrade it’s Dynamic Web Module to 3.0,which is currently 2.3.

1)If you will try to change it to 3.0 then you will get a message like:-
“Cannot change version of project facet Dynamic Web Module to 3.0”.

2016March16_1

2)Uncheck this with version i.e. 3.0 and click “Apply” and than click “OK”.

3)Again go to same screen i.e. Project Facets version to “3.0” will be already there and now make it checked now.

2016March16_2
Now Click on “OK” and then on “Apply”.You will be see its impact in project and will see that there is a new folder “WebContent”.
2016March16_3

4)Clean and build your project.Now you will see error as :-
2016March16_4

5)The reason for this is the that we need to change structure of web.xml for 3.0.
So Make your web.xml like below now:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xmlns="http://java.sun.com/xml/ns/javaee"
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
		 id="WebApp_ID"
		 version="3.0">

	<display-name>Archetype Created Web Application</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/rest-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>rest</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!--This Servlet mapping will handle all incoming requests -->
	<servlet-mapping>
		<servlet-name>rest</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
    

Now clean and build your project.Things should be working fine with web module 3.0.

Hope this will help someone.

Creating Restful Web Services In Java Using Spring 4.2.5

First of all just want to mention that I am going to Use Eclipse Kepler Release as IDE for this application.We will be creating this sample application as a base wrapper for an Enterprise application.We will be later on keep extending this sample to a certain level.Let’s also include maven implementation in this sample application.
I hope you will be already aware of Maven.If not just go through the link:-

https://en.wikipedia.org/wiki/Apache_Maven

In this sample application we will creating Restful web services which will just append ‘Hello’ and current date-time to our text which we will be passing by this web service.

Now Here is step by Step implementation of this sample application.

Step1 :-
a)In Eclipse go to File >> New >> Others >> Maven >> Maven Project.Then click Next.
b)Select “maven-archetype-webapp” Under ArtifactId as in below image and press ‘Next’.

2016March14_1
c)Under the group id name  enter ‘com.ssb.webSample’ and Aritfact Id as ‘SpringMavenRestDemoService’.

2016March14_2
d)Click on Finish.

You will be able to ‘SpringMavenRestDemoService’ application in your project list.Now open this application.
Step2:-

Open the ‘pom.xml’ of ‘SpringMavenRestDemoService‘ in text editor. And Replace it’s content with content below:-

<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.ssb.webSample</groupId>
  <artifactId>SpringMavenRestDemoService</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMavenRestDemoService</name>
  <url>http://maven.apache.org</url>
 
 <properties>
		<spring.version>4.2.5.RELEASE</spring.version>
		<jdk.version>1.7</jdk.version>
 </properties>
 
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	  <groupId>javax.servlet</groupId>
	  <artifactId>javax.servlet-api</artifactId>
	  <version>3.1.0</version>
	</dependency>
	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
	</dependency>
	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
	</dependency>
  </dependencies>

  <build>
		<finalName>SpringMavenRestDemoService</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
  
</project>

I am using latest spring version that is 4.2.5.

After this press right mouse click on project. Than Click on “Maven” and Click on “Update Project” and update by clicking on ‘OK’ button.

Step 3:-
Now Lets configure our ‘webapp’ as per below:-

a)web.xml inside ‘WEB-INF’:-

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
	  <servlet>
		 <servlet-name>rest</servlet-name>
		 <servlet-class>
		  org.springframework.web.servlet.DispatcherServlet
		 </servlet-class>
		 <load-on-startup>1</load-on-startup>
	  </servlet>

	<servlet-mapping>
		 <servlet-name>rest</servlet-name>
		 <url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

b)rest-servlet.xml inside ‘WEB-INF’:-

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.ssb.webSample.SpringMavenRestDemoService.controllers" />

</beans>

Step 4:-

Now Create a class with name ‘ServiceController’ under package “com.ssb.webSample.SpringMavenRestDemoService.controllers” inside a folder ‘java’.

2016March14_3

Below is code for that:-
package com.ssb.webSample.SpringMavenRestDemoService.controllers;

import java.util.Date;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping(&quot;/sayhello&quot;)
public class ServiceController {

 @RequestMapping(value = &quot;/{userName}&quot;, method = RequestMethod.GET)
 public String sayhello(@PathVariable String userName) {
  Date date = new Date();
  String strMsg =&quot;Hello &quot;+ userName + &quot; Curret Time is : &quot; + date.toString();
  return strMsg;
 }
}

Now build your application and run it.If all goes well you are ready with your service.
Just paste below url in your browser:-

http://localhost:8080/SpringMavenRestDemoService/sayhello/Robert

you can enter any name instead of ‘Robert’.

You Should be able to see the desired output.

 

Hope this will help someone.

 

Unable to publish on server Maven Spring project In Eclipse

With eclipse IDE if you are working with Maven project,you may get this type of exception:-

“Could not publish to the server.java.lang.IndexOutOfBoundsException

When was building my project in eclipse IDE and was trying to publish it to the sever was getting the above mentioned error message and was not able to run the application on server.

After trying multiple steps I was not to fix it.

For resolving this you will require to delete the Maven repository from your machine.

i.e. the .m2 folder which contains all dependencies required for a maven build.

And after that if you will build project from work-space dependencies will be reloaded again and you will able to publish it.

 

Hope this will help someone.

Creating Servlet with Eclipse IDE in Java

Lets go through step by step for creating servlet in Eclipse IDE.

1)Creating Project

 First of all create a dynamic web project in Eclipse, I am naming it ‘ServletTest’.

firstServlet1

firstServlet2

firstServlet4

After above steps your application will look like below:-

firstServlet5

2)Creating Servlet

Now create a servlet inside src folder as shown in below:-

firstServlet6

firstServlet7

Now you will be see here the code full of errors.So are here require to add proper .jar file.For this just go to ‘Java Build Path’ add add required jar file as below:-

firstServlet8

firstServlet9

firstServlet10

firstServlet11

Now you can see that all errors are already resolve after getting proper references as in below screens:-

firstServlet12

Now I have just modified doGet() method and added some small piece of code there as you can see in image below:-

firstServlet13

Now just build your application,things are ready to test on server.

3)Testing on Server

Just try like below:-

firstServlet16

firstServlet17

firstServlet18

firstServlet19

Thats all,we have succssfully created and tested our first servlet.

But if you will not able testing thing on server,and you see something like below image:-

firstServlet15

The reason for this may be possible that you have not setup server earlier ever.

To over come this you will require to setup your server first.As you can see in below images:-

server1

server2

server3

server4

Now as you have setup your server successfully you can now thy steps under ‘Testing on Server’.Hope you have now successfully tested your servlet.

Keep coding…….

Creating Web-Service in Java Using Eclipse

Just writing a basic small simple step by step implementation for creating web-service in Eclipse and creating client for testing web service. For this one must need to have Java EE version of eclipse.

Setup Application:

   First of all Create a dynamic web project I am naming it as ‘WebServiceTest’.

WSStep1

WSStep2

Creating Java Class As Web Service Provider :-

Now Inside ‘src’ folder just create ‘com’ folder and inside ‘com’ create a folder name as per you, I am just creating ‘bss’. This is just for a good practice as per standards.Now inside ‘bss’ folder I am creating a new class with Name ‘MathService’. Inside this class I am creating a method ‘addNumbers()’ which will be adding two number and will return their submission.

So the Overall Structure will look like below image:-

WSStep3

You have created your web service provider class, As you are using Eclipse, so all next steps are just few mouse clicks.

Building,Deploying and Testing:

Right Click on ‘Java Resources’ folder and go to web service as in below image:-

WSStep4

Select class by clicking on ‘Browse’ button for Service implementation as below:-

WSStep5

Drag both sliders under ‘Test Service’ and ‘Test Client’ to top. Here you can see during the drag that what exactly these are representing.Also make ‘Publish the Web service’ Check box true as below:-

WSStep6

And click on ‘Next’

WSStep7

Again Click on ‘Next’. You will be on below screen:-

WSStep8

Click on ‘Start Server’ button.After this carry on to Click ‘Next’ as in below screens:-

WSStep9

WSStep10

Here in above image you will be see this ‘Launch’ button, currently don’t click on this. I will be explain at last that what is purpose of this.

WSStep11

WSStep12

WSStep13

Finally in above screen click on ‘Finish’.After clicking on Finish you will be see what you need. You can see in browser that .jsp file is there for testing your web service, you can test your methods and see expected result as in below images:-

WSStep14

WSStep15

So that’s all here. But don’t forgot to explore each and every class which are created inside ‘WebServiceTest‘ and ‘WebServiceTestClient‘ for actual implementation.

One additional test approach is also there:-

As above at once place there was a button ‘Launch’,by clicking on that also we can test  web service,in that case you can see result as below:-

WsdlTestStep1

WsdlTestStep2

Hope this will help someone.

Enjoy………………

Just want to add an additional information that some times  during this exercise you may find an Exception like this:-

java.lang.Exception: Couldn’t find a matching Java operation for WSDD operation YourMehodName

This is bit confusing….. The only reason for this is that inside your .java class you have written method name starting with capital letter.If in our case we have written method name as AddNumbers() then we will get this error. So for avoiding this simply make first character small in your method name.

Below is full trace of this Exception:-

java.lang.Exception: Couldn’t find a matching Java operation for WSDD operation “addNumbers” (2 args)  at org.apache.axis.InternalException.<init>(InternalException.java:71)  at org.apache.axis.description.JavaServiceDesc.loadServiceDescByIntrospection(JavaServiceDesc.java:902)  at org.apache.axis.providers.java.JavaProvider.initServiceDesc(JavaProvider.java:477)  at org.apache.axis.handlers.soap.SOAPService.getInitializedServiceDesc(SOAPService.java:286)  at org.apache.axis.deployment.wsdd.WSDDService.makeNewInstance(WSDDService.java:500)  at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)  at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)  at org.apache.axis.deployment.wsdd.WSDDDeployment.getService(WSDDDeployment.java:427)  at org.apache.axis.configuration.FileProvider.getService(FileProvider.java:231)  at org.apache.axis.AxisEngine.getService(AxisEngine.java:311)  at org.apache.axis.MessageContext.setTargetService(MessageContext.java:756)  at org.apache.axis.handlers.http.URLMapper.invoke(URLMapper.java:50)  at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)  at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)  at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)  at org.apache.axis.server.AxisServer.invoke(AxisServer.java:239)  at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)  at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  at java.lang.Thread.run(Unknown Source)

 

 

Keep Coding……….

 

 

 

Eclipse Not Working Only Flickering

If you are finding issue somethig like this,then first of all just go to your command prompt under username and check java version there
by simply typing:-

java -version

If you can see java version there then simply checking below settings:-

JAVA Installation:

Installed JDK at C:\Program Files\Java\jdk1.7.0_10
Installed JRE at C:\Program Files\Java\jre7

Environment Variables Configuration:

JAVA_HOME = C:\Program Files\Java\jdk1.7.0_10
PATH = C:\Program Files\Java\jdk1.7.0_10\bin;

But if you are unable to see java version there and some error like:-

“Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object”

Then just go to :-
C:\Program Files\Java\jdk1.7.0_10\jre\lib

and check that “rt.jar” is there.If this is not there then uninstall java from your machine and install java again.

Hope fully it will help someone.

keep coding………….