2017年9月15日 星期五

Principle of Jersey


1. Basci principle
REST is an architectural style which is based on web-standards and the HTTP protocol.

REST allows that resources have different representations, e.g., text, XML, JSON etc. The REST client can ask for a specific representation via the HTTP protocol (content negotiation


The PUTGETPOST and DELETE methods are typical used in REST based architectures. The following table gives an explanation of these operations.
  • GET defines a reading access of the resource without side-effects. The resource is never changed via a GET request, e.g., the request has no side effects (idempotent).
  • PUT creates a new resource. It must also be idempotent.
  • DELETE removes the resources. The operations are idempotent. They can get repeated without leading to different results.
  • POST updates an existing resource or creates a new resource.

Reference:  article




2. About jersey 

Jersey  is
2.1. JAX-RS
Java defines REST support via the Java Specification Request (JSR) 311. This specification is called JAX-RS (The Java API for RESTful Web Services). JAX-RS uses annotations to define the REST relevance of Java classes.

2.2. Jersey

The Jersey implementation provides a library to implement Restful webservices in a Java servlet container. It is the reference implementation for the JSR 311 specification.
Jersey provides a servlet implementation which scans predefined classes to identify RESTful resources. You use annotations in the class and methods to define their responsibility.

2.3 Histroy
Biggest difference between Jersey 1 and Jersey 2 in this regard is used namespace. Jersey 1 uses prefix com.sun.jersey (even for property names) which is not true for the namespace of Jersey 2 that starts with org.glassfish.jersey. Besides that Jersey 2 does NOT support properties from Jersey 1 and you need to use specific ones (from Jersey 2) to make things work. ref


3. Jersey annotation

Annotation:J2SE 5.0 中對 metadata 提出的功能是 Annotation,metadata 就是「資料的資料」(Data about data),突然看到這樣的解釋會覺得奇怪,但以表格為例,表格中呈現的就是資料,但有時候還會有額外的資料用來說明表格的作用,從這個角度來看,metadata 就不這麼的奇怪。
在 J2SE 5.0 中,Annotation 的主要目的介於原始碼與 API 文件說明之間,Annotation 對程式碼作出一些說明與解釋,Class 中可以包含這些解釋,編譯器或其它程式分析工作可以使用 Annotation 來作分析,您可以從 java.lang.Override、java.lang.Deprecated、java.lang.SuppressWarnings 這三個 J2SE 5.0 中標準的 Annotation 型態開始瞭解 Annotation 的作用。 ref

AnnotationDescription
@PATH(your_path)
Sets the path to base URL + /your_path. The base URL is based on your application name (the context root), the servlet and the URL pattern from the web.xml configuration file.
@POST
Indicates that the following method will answer to an HTTP POST request.
@GET
Indicates that the following method will answer to an HTTP GET request.
@PUT
Indicates that the following method will answer to an HTTP PUT request.
@DELETE
Indicates that the following method will answer to an HTTP DELETE request.
@Produces(MediaType.TEXT_PLAIN[, more-types])
@Produces defines which MIME type is delivered by a method annotated with @GET. In the example text ("text/plain") is produced. Other examples would be "application/xml" or "application/json".
@Consumes(type[, more-types])
@Consumes defines which MIME type is consumed by this method.
@PathParam
Used to inject values from the URL into a method parameter. This way you inject, for example, the ID of a resource into the method to get the correct object.
Reference: article.html



4. Return  code 
  • 1xx: Informational - Communicates transfer protocol-level information
  • 2xx: Success -Indicates that the client’s request was accepted successfully.
  • 3xx: Redirection - Indicates that the client must take some additional action in order to complete their request.
  • 4xx: Client Error - This category of error status codes points the finger at clients.
  • 5xx: Server Error - The server takes responsibility for these error status codes.






T. Jersey Example 

2017年8月29日 星期二

Learning Jersey in web Step


1 First  jersey project 

  • eclipse Mar2 Release - 內含 M2E(Maven Plugin)
  • Tomcat 8.0.22
  • Jersey 2.17


a. New -> Maven project -> maven-archetype-webapp

b. Group Id:  tw.com.edittw
    artifactId: jerseySample

c. Setting target runtimes: apache tomcatV8.0

d. Select maven -> add dependency


e.  add the information
  • Group Id : org.glassfish.jersey.containers
  • Artifact Id : jersey-container-servlet
  • Version : 2.17

f. Run maven install




Ref: Frist Jersey


2 Hello Jersey


1. Setting web.xml 

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<!-- Add the jersey to start -->
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Add the package that will search for path in com.javatpoint.rest -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javatpoint.rest</param-value>
</init-param>
<!-- In need you can add logging -->
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<!-- Enable Tracing support. -->
<init-param>
<param-name>jersey.config.server.tracing</param-name>
<param-value>ALL</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Mapping all the /rest/* files and request to the jersey -->
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
2. Programming the java and index link

And  the first hello jersey
Note it is using the GET method


Type the Simple hello will show the message



















2017年8月25日 星期五

Maven

Install on windows 

1.  Download Java JDK for 1.7 version  in

http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html


2.  Seting  JAVA_HOME=C:\Program Files\Java\jdk1.7.0_80


3. Add M2_HOME and MAVEN_HOME
Add both M2_HOME and MAVEN_HOME variables in the Windows environment, and point it to your Maven folder.




Reference:
http://roufid.com/no-compiler-is-provided-in-this-environment/
https://www.mkyong.com/maven/how-to-install-maven-in-windows/


Running simple maven my-app

This will build the project call my-app and 
  1. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


  • project This is the top-level element in all Maven pom.xml files.
  • modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
  • groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plugins.
  • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
  • packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
  • version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
  • name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
  • url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
  • description This element provides a basic description of your project. This is often used in Maven's generated documentatio

Build project
  1. mvn package 會製作出 jar 檔

2. mvn compile

3. mvn exec:java -Dexec.mainClass=com.mycompany.app.App

4. man install  store in the local repo




Running maven web

Command:
mvn archetype:generate -DgroupId=com.myMaven.app -DartifactId=HelloJavaWeb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

mvn package : 會建立 war 檔

mvn jetty:run


mvn jetty:run

jetty reference: https://www.mkyong.com/maven/maven-jetty-plugin-examples/




How to do  add other jar file

增加相關jar檔

當我們想要使用一些套件時,這時候Maven要怎麼做呢?
舉例來說,想使用mysql的時候,
就把mysql的dependency資訊加入pom.xml檔
Maven會幫我們把這些jar檔一起封裝進來 

修改pom.xml檔,加入 mysql的套件資訊

dependency的套件資訊可以在下列位置找到
http://mvnrepository.com/ 



加入 dependency 到 pom.xml

如何找套件

建立套件並自動下載

war 放置 jar


舉例來說,如果我們想使用A套件
但使用A套件必須要有B套件跟C套件,
這時候我們不必費心去找B,C套件,
甚至連A套件需要什麼相依套件我們都不用知道
只要在pom.xm中設定,
Maven都會幫我們準備好.



Running with eclipse  .將Maven專案轉成eclipse專案

要導入一個已經存在的Maven專案
我們必須要先把Maven專案轉成Eclipse可以使用的專案格式
在Maven專案的資料夾內(pom.xml存在的那層)輸入

mvn eclipse:eclipse
Maven會產生Eclipse需要的檔案 







Eclipse 使用的是 eclipse-jee-mars-2-win32-x86_64

開啟 Eclipse 並 import 轉好 maven 檔案






加入 log4j 至 Pom.xml






Eclipse Setting Maven 



Nothing special, M2_REPO is just a normal “classpath variable” in Eclipse to find your local Maven repository.

Eclipse 製作 Maven project



選定建立目錄

 
專案設定
 
執行一樣用 package
加入 javax.servlet
設定 servrlet in the main