Jersey WebTarget vs HTTPURLConnection - java

I'm learning web services using Java. I'm technically a noob, this is the code I've written, which works, I just don't know which method has what advantage over another, like which one is more secure? Which one will be more faster?
I'm not asking for complete too broad an answer. A short concise one will do.
I've created a REST service using Jersey 2.x and I've created client to consume the said REST service.
POST resource is as follows,
#POST
#Path("postactivity")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public String createActivity(
#QueryParam("id") int id,
#QueryParam("description") String description,
#QueryParam("duration") int duration,
#QueryParam("name")String name)
{
//My code that creates Activity object from QueryParams is here.
}
Now I've created a Client which is a Java Application. I'm consume the above REST Service above in following two ways.
Method 1 Using HTTPURLConnection
private static void doPost(){
QueryString qs = new QueryString("id", "123"); //QueryString is a class created to build query, not important to the question.
qs.add("duration", "12");
qs.add("description", "This is description");
qs.add("name", "This is Name");
String url = "http://localhost:8080/webservices/webapi/activities/activity?" + qs;
URL obj;
try {
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Content-Type","application/json");
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla 5.0");
con.setDoOutput(true);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Activity activity = GSON.fromJson(response.toString(), Activity.class); //This is for checking if i'm getting correct data back which I'm sending.
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Method 2 Using WebTarget available via Jersey
private static void doPost(){
Response entity = webTarget
.path("activities/postactivity")
.queryParam("id",2204)
.queryParam("description","Foo")
.queryParam("duration",100)
.queryParam("name", "Bar")
.request()
.post(null);
String entityRead = entity.readEntity(String.class);
System.out.println(entityRead);
Activity activityRead = GSON.fromJson(entityRead, Activity.class);
}
Thanks.

Honestyl i have two things to write to you:
1. The HttpURLConnection is the Java personal way of doing a retrieve of a web affiliate (like web services) but you have a better and stress-free way of doing it with Jersey and this will make things faster and smoother for you. For some persons they even say that the Jersey style is the High-level API while the HttpURLConnection is called the low-level API.
2. Your question was able to provide me with a necessary solution to a problem i have had for the past two days with consuming a #Queryparam POST webmethod. I really appreciate this.
Thanks

Related

How to call a GraphQL query/mutation with Java code

I know we can use the /graphiql endpoint to test the created or deployed GraphQL API.
I need to consume a GraphQL API which is already there using a Java code. How can I do it.
How can I test a GraphQL end-point using a postman like application, or is it even possible.
Depending on your situation, utilizing Spring RestTemplate might be simplest thing to do.
public class Person {
#JsonProperty("query")
public final String query = "mutation($name:String, $gender:String, $age:Int) { updateDetails (name: $name, gender: $gender, age: $age) }";
#JsonProperty("variables")
public final Map<String, String> variables;
...
}
...
(new RestTemplate()).postForEntity("http://example.com/graphql", new Person("David", "male", 12));
Could go even simpler by avoiding Spring and using HttpClient, building same JSON document using StringBuilder.
Alternatively Apollo Andoid can autogenerate client code for you.
An example, to work for me...
username and password are wrong :) But, URL is ok...
with that, I don't use RestTemplate, any jars, etc..
private static void test_LivePharma() {
try {
URL url = new URL("https://stores-hml.funcionalmais.com/graphql");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
String input="{\"query\":\"mutation{createToken(login:\\\"112233\\\",password:\\\"112233\\\"){ token}}\\n\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
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();
}
}
Consuming graphql API in your backend code, will require multiple steps such as.
Defining the schema
Installing the dependencies
Defining the graphql endpoint
import com.coxautodev.graphql.tools.SchemaParser;
import javax.servlet.annotation.WebServlet;
import graphql.servlet.SimpleGraphQLServlet;
#WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {
public GraphQLEndpoint() {
super(SchemaParser.newParser()
.file("schema.graphqls") //parse the schema file created earlier
.build()
.makeExecutableSchema());
}
}
Here is a detailed guide to do the same : Graphql-java-backend-implementation
Yes, it is possible to send the request using postman. You can simply copy the schema in the body.
Doing GraphQL request using Postman

How can I extend HttpURLConnection to support other methods?

I want to build a protocol on top of http (like WebDAV). So it makes sense to use HttpURLConnection because it contains all basic methods, response codes and so on. Unfortunately it prevents setting other methods like:
HttpURLConnection con = URL.openConnection( "http://abc.def/someServlet" );
con.setRequestMethod( "MyMethod" );
throws a ProtocolException Invalid method MyMethod.
So I extended it and overwrote "setRequestMethod( String )" to accept "MyMethod". This does not work, because URL only returns a HttpURLConnection. Is there any way to use HttpURLConnection to accept other methods?
Please don't, I tell you this because I did the same error when I started programming in java and it did not pay off.
Ask yourself this question, will you ever pass the child class to a function / method / constructor which accepts the parent class?
For example will you put in a collection a mix of HttpURLConnection and MyServiceURLConnection.
If not so, you will be better off with a brand new class which uses a HttpURLConnection to provide the low level operations.
You might wish to consider RESTful interface. You still use http werbs but you are level up, don't see it, can do any interface, use factories, like:
public String readFileAsString(String url) {
String result = null;
try {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.type("text/plain").get(ClientResponse.class);
result= response.getEntity(String.class);
response.close();
} catch (Exception e) {
e.printStackTrace();
result = "";
}
return result;
}
url dosent return HTTPURLConnection , it returns URLConnection because you didnt cast it
so you cannot override the HTTPURlConnection method setRequestMethod()

jquery ajax returns element not found

I have a REStful webservice (java, jersey) to do some stuff.
every function that calls an ajax request (GET or POST) with an url to the REST controller works fine... except of the recent ones and I do not know why.. i tried everything and stuck with this problem for nearly three days (wrote 3 different functions, changed from GET to POST, rewrote the function with new pathannotiation, tried to call on pageload.. renamed everything), I realy appreciate ANYTHING that could help me...
if the url contains rest/* the controller forwards it to the class which implements the needed functions..
JS function
function testFunc() {
$.ajax({
url: "rest/name/wut",
type: "GET",
contentType: "application/json;charset=utf-8",
success: function(response) {
alert("LSKDFJLSDKJFLKSD " + response);
},
error: function(response) {
alert("ma oidaaaa " + JSON.stringify(response));
}
});
};
Java Code in the RESTClass...
#GET
#Path("/wut")
#Produces(MediaType.APPLICATION_JSON)
private String wut() {
JSONObject json = new JSONObject();
json.put("print", "wuut");
return json.toString();
}
It does not matter if the method is doing anything useful or not... it just returns 404 no element found.
(it is not even called) Therefore i tried different new methods in the RESTClass... like:
#GET
#Path("/wut")
#Produces(MediaType.APPLICATION_JSON)
private String wut() throws IOException {
URL url = new URL(url);
StringBuffer response = new StringBuffer();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
I am using this snipped in another method, which is working.. I replaced the real URL with "url" for posting btw.
I also tried different MediaTypes: WILDCARD, TEXT/PLAIN...
And to just return a String...
Anyone any ideas (and SORRY for the bad english, I am really desperate so i did not do a spellcheck and english is not my native :( )
Two ideas:
First, declare your wut() method as public: public String wut()
Second, try to call your method in a browser, for example http://localhost/rest/name/wut and see what happens
I would try to use an absolute path:
Change:
url: "rest/name/wut",
to
url: "/rest/name/wut",
The error message tells me that, your client did not try the address the server provides.

jax-rs returned String

I am programming a jax-rs webservice which I want to contact from a java-program. I defined a #POST method which receives a String-array as input and is supposed to return a boolean value.
But really, how can i access this return value in java? I've been searching the web for several hours now, everybody writes example methods which return Strings or something else, but nobody shows how to access the returned value from another java program.
Here is the code from the program that contacts the #POST method:
ObjectOutputStream oos = null;
String[] login = {"XXXXXX","XXXXXXX"};
try {
login[1] = PasswordGenerator.hashPassword(login[1]);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URL url = new URL("XXXXX/XXXXXXX/XXXXXX/users/login/1");
try {
// creates a HTTP connection
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Boolean", "application/xml");
OutputStream outputStream = httpConn.getOutputStream();
oos = new ObjectOutputStream(outputStream);
outputStream.close();
System.out.println();
} finally {
if (oos != null) {
oos.close();
}
}
}
What I want to know is: What happens after I closed my outputStream? I mean, i started the POST method, but it is supposed to return a boolean value. Where is this value? How can I access it???
JAX-RS 2.0 has a Client API that provides you with a fluent API to retrieve the content of the response:
Client client = ClientBuilder.newClient();
Boolean result = client.target("http://localhost:8080/xxx/")
.path("user/login/1")
.request(MediaType.TEXT_PLAIN_TYPE)
.post(Entity.entity(login, MediaType.APPLICATION_XML) , Boolean.class);
But unfortunately, you'll need a custom MessageBodyWriter to convert the String[] into an XML document. Maybe you should change your server-side method (and client) to manipulate a DTO - a POJO with 2 fields, username and password - and annotated with #XmlRootElement ?
something like that:
(client-side)
Credentials credentials = new
credentials.setUsername("foo");
credentials.setUsername("hashedPwd");
Client client = ClientBuilder.newClient();
Boolean result = client.target("http://xxxxx")
.path("/user/login/1")
.request(MediaType.TEXT_PLAIN_TYPE)
.post(Entity.entity(credentials, MediaType.APPLICATION_XML) , Boolean.class);
System.out.println("Result: " + result);
(server-side)
#Path("/login/{id}")
#POST
public Boolean test(#PathParam("id") String login, Credentials credentials) {
...
}

Send and receive JSON to REST WebService in Jersey Java

I am new to Jersey Java REST WebService framework. I am trying to write a service method which consumes and produces JSON. My service code is below. It is simplest code, just for studying purpose.
#Path("/myresource")
public class MyResource {
#Path("/sendReceiveJson")
#GET
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String sendReceiveJson(String name)
{
System.out.println("Value in name: " + name);
return "{\"serviceName\": \"Mr.Server\"}";
}
}
And following is JerseyClient code.
public class Program {
public static void main(String[] args) throws Exception{
String urlString="http://localhost:8080/MyWebService/webresources/myresource/sendReceiveJson";
URL url=new URL(urlString);
URLConnection connection=url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("{\"clientName\": \"Mr.Client\"}");
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}
But when i run service and then client, i am unable to send/receive JSON data. I get Exception at connection.getInputStream() which is
Server returned HTTP response code: 405 for URL: http://localhost:8080/hellointernet/webresources/myresource/sendReceiveJson
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
Please guide me, what needs to correct, or whether i am in wrong direction.
Your resource method is annotated as #GET which means any input data would have to be query string parameters.
In this context #Consumes(MediaType.APPLICATION_JSON) doesn't make a lot of sense as only APPLICATION_FORM_URLENCODED is supported via GET.
When you client calls setDoOutput(true) it probably switches your HTTP call to a POST hence causing the 405 Method Not Allowed.
If you want to consume JSON you should change your #GET annotation with #POST instead. Your client call should then work if it's indeed a POST. You can specify it with the following method:
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
This API is pretty low level though, so I'd highly recommend you use Jersey's Client API instead. See https://jersey.java.net/documentation/1.17/client-api.html

Categories