technicise-software-and-technologies / pixConsumer-java

MIT License
0 stars 1 forks source link

generate REST webservice using spring web services #2

Open sayan801 opened 7 years ago

sayan801 commented 7 years ago

https://spring.io/guides/gs/rest-service/

sunandit-cribello commented 7 years ago

Documentaion for the process of creating a "hello world" RESTful web service with Spring

Clone the web service named as gs-rest-service

user@user-Vostro-3550: cd /home/user/code
user@user-Vostro-3550: git clone https://github.com/spring-guides/gs-rest-service.git

How to build gs-rest-service using Maven

user@user-Vostro-3550:~/code$ cd /home/user/code/gs-rest-service/
user@user-Vostro-3550:~/code/gs-rest-service$ pwd
/home/user/code/gs-rest-service
user@user-Vostro-3550:~/code/gs-rest-service$ ls
complete  CONTRIBUTING.adoc  initial  LICENSE.code.txt  LICENSE.writing.txt  README.adoc  run-on-pws.json  test
user@user-Vostro-3550:~/code/gs-rest-service$ cd complete
user@user-Vostro-3550:~/code/gs-rest-service/complete$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-rest-service 0.1.0
[INFO] ------------------------------------------------------------------------
........

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.079 s
[INFO] Finished at: 2017-02-26T22:42:49+05:30
[INFO] Final Memory: 27M/264M
[INFO] ------------------------------------------------------------------------
sunandit-cribello commented 7 years ago

How to run gs-rest-service

Using Eclipse project with Eclipse Neon version 4.6.2

user@user-Vostro-3550:~/code/gs-rest-service/complete$ pwd
/home/user/code/gs-rest-service/complete
user@user-Vostro-3550:~/code/gs-rest-service/complete$ mvn eclipse:clean
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-rest-service 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-eclipse-plugin:2.10:clean (default-cli) @ gs-rest-service ---
[INFO] Deleting file: .project
[INFO] Deleting file: .classpath
[INFO] Deleting file: .wtpmodules
[INFO] Deleting file: .settings
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.658 s
[INFO] Finished at: 2017-02-26T22:50:26+05:30
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------
user@user-Vostro-3550:~/code/gs-rest-service/complete$ mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-rest-service 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ gs-rest-service >>>
[INFO] 
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ gs-rest-service <<<
[INFO] 
[INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ gs-rest-service ---
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "gs-rest-service" to /home/user/code/gs-rest-service/complete.
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.721 s
[INFO] Finished at: 2017-02-26T22:50:36+05:30
[INFO] Final Memory: 17M/310M
[INFO] ------------------------------------------------------------------------

To import the project into Eclipse navigate to File->Import->General->Existing Projects into Workspace in the Eclipse menu and select the created folder complete as root directory from /home/user/code/gs-rest-service/complete.

sunandit-cribello commented 7 years ago

How to run gs-rest-service

Using Maven

user@user-Vostro-3550:~/code/gs-rest-service/complete$ pwd
/home/user/code/gs-rest-service/complete
user@user-Vostro-3550:~/code/gs-rest-service/complete$./mvnw spring-boot:run

It Shows:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

2017-02-26 22:57:38.598  INFO 3709 --- [           main] hello.Application                        : Starting Application on user-Vostro-3550 with PID 3709 (/home/user/code/gs-rest-service/complete/target/classes started by user in /home/user/code/gs-rest-service/complete)
2017-02-26 22:57:38.604  INFO 3709 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2017-02-26 22:57:38.690  INFO 3709 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c1bf502: startup date [Sun Feb 26 22:57:38 IST 2017]; root of context hierarchy
2017-02-26 22:57:39.951  INFO 3709 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-26 22:57:40.090  INFO 3709 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-26 22:57:40.785  INFO 3709 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-02-26 22:57:40.807  INFO 3709 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-02-26 22:57:40.809  INFO 3709 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-02-26 22:57:40.929  INFO 3709 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-02-26 22:57:40.930  INFO 3709 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2246 ms
2017-02-26 22:57:41.152  INFO 3709 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-02-26 22:57:41.159  INFO 3709 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-02-26 22:57:41.160  INFO 3709 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-02-26 22:57:41.161  INFO 3709 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-02-26 22:57:41.161  INFO 3709 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-02-26 22:57:41.568  INFO 3709 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c1bf502: startup date [Sun Feb 26 22:57:38 IST 2017]; root of context hierarchy
2017-02-26 22:57:41.672  INFO 3709 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)
2017-02-26 22:57:41.677  INFO 3709 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-02-26 22:57:41.678  INFO 3709 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-26 22:57:41.713  INFO 3709 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 22:57:41.714  INFO 3709 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 22:57:41.761  INFO 3709 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 22:57:41.961  INFO 3709 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-02-26 22:57:42.039  INFO 3709 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-26 22:57:42.051  INFO 3709 --- [           main] hello.Application                        : Started Application in 4.092 seconds (JVM running for 9.193)
sunandit-cribello commented 7 years ago

How to run gs-rest-service

Using JAR file

user@user-Vostro-3550:~/code/gs-rest-service/complete$ pwd
/home/user/code/gs-rest-service/complete
user@user-Vostro-3550:~/code/gs-rest-service/complete$./mvnw clean package
user@user-Vostro-3550:~/code/gs-rest-service/complete$java -jar target/gs-rest-service-0.1.0.jar

It shows:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

2017-02-26 23:10:02.355  INFO 4135 --- [           main] hello.Application                        : Starting Application v0.1.0 on user-Vostro-3550 with PID 4135 (/home/user/code/gs-rest-service/complete/target/gs-rest-service-0.1.0.jar started by user in /home/user/code/gs-rest-service/complete)
2017-02-26 23:10:02.362  INFO 4135 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2017-02-26 23:10:02.502  INFO 4135 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9af9a9: startup date [Sun Feb 26 23:10:02 IST 2017]; root of context hierarchy
2017-02-26 23:10:04.123  INFO 4135 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-26 23:10:04.288  INFO 4135 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-26 23:10:04.836  INFO 4135 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-02-26 23:10:04.862  INFO 4135 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-02-26 23:10:04.864  INFO 4135 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-02-26 23:10:05.027  INFO 4135 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-02-26 23:10:05.028  INFO 4135 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2531 ms
2017-02-26 23:10:05.262  INFO 4135 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-02-26 23:10:05.269  INFO 4135 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-02-26 23:10:05.270  INFO 4135 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-02-26 23:10:05.270  INFO 4135 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-02-26 23:10:05.271  INFO 4135 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-02-26 23:10:05.775  INFO 4135 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9af9a9: startup date [Sun Feb 26 23:10:02 IST 2017]; root of context hierarchy
2017-02-26 23:10:05.917  INFO 4135 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)
2017-02-26 23:10:05.924  INFO 4135 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-02-26 23:10:05.925  INFO 4135 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-26 23:10:05.977  INFO 4135 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 23:10:05.978  INFO 4135 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 23:10:06.052  INFO 4135 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-26 23:10:06.292  INFO 4135 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-02-26 23:10:06.378  INFO 4135 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-26 23:10:06.386  INFO 4135 --- [           main] hello.Application                        : Started Application in 4.625 seconds (JVM running for 5.26)
sunandit-cribello commented 7 years ago

Output JSON from gs-rest-service

image


image

sunandit-cribello commented 7 years ago

How to check which process running @ localhost server 8080 port

user@user-Vostro-3550:~$ sudo netstat -lpn |grep :8080
tcp6       0      0 :::8080                 :::*                    LISTEN      4135/java       

-- It shows a process with PID 4135 running @ port 8080

How to kill a process running @ localhost server 8080 port

user@user-Vostro-3550:~$ kill 4135
user@user-Vostro-3550:~$ sudo netstat -lpn |grep :8080
----- For force kill use below
user@user-Vostro-3550:~$ kill -9 4135 
shantu commented 7 years ago

Create a deployable war file

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container. Typically, you update your application’s main class to extend SpringBootServletInitializer: In case of example from previous comments, modify the Application.java

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

The next step is to update your build configuration so that your project produces a war file rather than a jar file. If you’re using Maven and using spring-boot-starter-parent (which configures Maven’s war plugin for you) all you need to do is modify pom.xml to change the packaging to war:

<packaging>war</packaging>

If you’re using Gradle, you need to modify build.gradle to apply the war plugin to the project:

apply plugin: 'war'

The final step in the process is to ensure that the embedded servlet container doesn’t interfere with the servlet container to which the war file will be deployed. To do so, you need to mark the embedded servlet container dependency as provided.

If you’re using Maven:

<dependencies>
    <!-- … -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- … -->
</dependencies>

And if you’re using Gradle:

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}
shantu commented 7 years ago

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file