Rest API Post Example With Client in Java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
/**
*
* @author ananddw
*/
public class Accounts {
String accountid;
String accountname;
String accountholdername;
String accountholderlocation;
public String getAccountid() {
return accountid;
}
public void setAccountid(String accountid) {
this.accountid = accountid;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public String getAccountholdername() {
return accountholdername;
}
public void setAccountholdername(String accountholdername) {
this.accountholdername = accountholdername;
}
public String getAccountholderlocation() {
return accountholderlocation;
}
public void setAccountholderlocation(String accountholderlocation) {
this.accountholderlocation = accountholderlocation;
}
@Override
public String toString() {
return "Product [accountname=" + accountname + ", accountholderlocation=" + accountholderlocation + ", accountholdername=" + accountholdername + ", accountid=" + accountid + "]";
}
}
=======================
@Path("/accounts")
public class JSONService {
@POST
@Path("/account")
@Consumes("application/json")
public Response getAccountsInJSON(Accounts account) {
String result ="Account created : "+account;
return Response.status(201).entity(result).build();
}
}
=======================
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author ananddw
*/
public class JSONUtility {
public static void main(String[] args) {
JSONUtility obj = new JSONUtility();
obj.run();
}
public String run() {
ObjectMapper mapper = new ObjectMapper();
Accounts staff = createDummyObject();
String jsonInString = null;
try {
jsonInString = mapper.writeValueAsString(staff);
System.out.println(jsonInString);
String preetyJSON = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff);
} catch (Exception e) {
e.printStackTrace();
}
return jsonInString;
}
public Accounts createDummyObject() {
Accounts staff = new Accounts();
staff.setAccountholderlocation("Bangalore");
staff.setAccountholdername("Anand");
staff.setAccountid("Anand-MSPYL");
staff.setAccountname("HDFC");
return staff;
}
}
====================================
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Admin
*/
public class AccountClientPost {
public static void main(String[] args) {
try {
URL url = new URL(
"http://localhost:8084/RESTAPI/rest/accounts/account");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
JSONUtility obj = new JSONUtility();
String input= obj.run();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
===============web.xml ==============
<?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"
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>User Management</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Please uploaded jersay and Jackson jars .
Happy to help Thanks
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
/**
*
* @author ananddw
*/
public class Accounts {
String accountid;
String accountname;
String accountholdername;
String accountholderlocation;
public String getAccountid() {
return accountid;
}
public void setAccountid(String accountid) {
this.accountid = accountid;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public String getAccountholdername() {
return accountholdername;
}
public void setAccountholdername(String accountholdername) {
this.accountholdername = accountholdername;
}
public String getAccountholderlocation() {
return accountholderlocation;
}
public void setAccountholderlocation(String accountholderlocation) {
this.accountholderlocation = accountholderlocation;
}
@Override
public String toString() {
return "Product [accountname=" + accountname + ", accountholderlocation=" + accountholderlocation + ", accountholdername=" + accountholdername + ", accountid=" + accountid + "]";
}
}
=======================
@Path("/accounts")
public class JSONService {
@POST
@Path("/account")
@Consumes("application/json")
public Response getAccountsInJSON(Accounts account) {
String result ="Account created : "+account;
return Response.status(201).entity(result).build();
}
}
=======================
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author ananddw
*/
public class JSONUtility {
public static void main(String[] args) {
JSONUtility obj = new JSONUtility();
obj.run();
}
public String run() {
ObjectMapper mapper = new ObjectMapper();
Accounts staff = createDummyObject();
String jsonInString = null;
try {
jsonInString = mapper.writeValueAsString(staff);
System.out.println(jsonInString);
String preetyJSON = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff);
} catch (Exception e) {
e.printStackTrace();
}
return jsonInString;
}
public Accounts createDummyObject() {
Accounts staff = new Accounts();
staff.setAccountholderlocation("Bangalore");
staff.setAccountholdername("Anand");
staff.setAccountid("Anand-MSPYL");
staff.setAccountname("HDFC");
return staff;
}
}
====================================
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Admin
*/
public class AccountClientPost {
public static void main(String[] args) {
try {
URL url = new URL(
"http://localhost:8084/RESTAPI/rest/accounts/account");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
JSONUtility obj = new JSONUtility();
String input= obj.run();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
===============web.xml ==============
<?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"
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>User Management</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Please uploaded jersay and Jackson jars .
Happy to help Thanks
Comments
Post a Comment