java - Invoking JAX-RS service from a servlet -


i'm working on java web application using eclipse. have created web page servlet asks user choose file upload, , have web service want handle file.

my issue how pass parameters servlet web service, , invoke service servlet.

i have tried methods using context , httpconnections neither seemed have work.

any or advice appreciated!

my code servlet:

protected void dopost(httpservletrequest request,         httpservletresponse response) throws servletexception, ioexception {     httpsession session = request.getsession(false);      string uploadfile = request.getparameter("uploadfile");      string username = (string)session.getattribute("loginname");      url url = new url ("http://localhost:9763/cwengine_1.0.0/services/engine");     httpurlconnection conn = (httpurlconnection) url.openconnection();     conn.setdooutput(true);     conn.setrequestmethod("post");     conn.setrequestproperty("content-type", mediatype.text_plain);      map<string, string> uname = new hashmap<string, string>();      map<string, string> ufile = new hashmap<string, string>();      string output = uname.put("loginname", username) + ufile.put("ufile", uploadfile);      outputstreamwriter os = new outputstreamwriter(conn.getoutputstream());     os.write(output);     os.close(); } 

my code web service:

@path("/") public class engine {  @context private static servletcontext context;  @post @consumes(mediatype.text_plain)      public void main(string[] args) {          url url = null;         httpurlconnection connection = null;          try {             url = new url ("http://localhost:9763/cwengine_1.0.0/services/engine");         } catch (malformedurlexception e1) {             // todo auto-generated catch block             e1.printstacktrace();         }          try {             connection = (httpurlconnection) url.openconnection();         } catch (ioexception e1) {             // todo auto-generated catch block             e1.printstacktrace();         }          bufferedreader input = null;          try {             input = new bufferedreader(new inputstreamreader(connection.getinputstream()));         } catch (ioexception e1) {             // todo auto-generated catch block             e1.printstacktrace();         }          //string output = uname.put("loginname", username) + ufile.put("ufile", uploadfile);          map<string, string> uname = (map<string, string>) ((servletcontext) input).getattribute("uname");         string username = uname.get("loginname");          map<string, string> ufile = (map<string, string>) context.getattribute("ufile");         string uploadfile = ufile.get("ufile");          system.out.println(uploadfile + username); 

you can build rs client in servlet

first, have build rs client

javax.ws.rs.client.client client = clientbuilder.newclient(); 

then point restful service url

webtarget target = client.target("your restful service url"); 

and specify kind of data prefer get, take xml example

builder builder = target.request(mediatype.application_xml); 

and last specify http method, take example

type returnobj = builder.get(type);  

once builder.get(type) executed, return object type "type". "type" must same return type of restful service.

in case, need use

builder.post(entity.entity("your input object", "the media type")); 

i use jersey most, , here official site further reference client api. https://jersey.java.net/documentation/latest/client.html


i'll give simple runnable example

my pom dependency

<dependencies>         <!-- jackson json dependency -->         <dependency>             <groupid>com.fasterxml.jackson.core</groupid>             <artifactid>jackson-core</artifactid>             <version>2.6.5</version>         </dependency>         <dependency>             <groupid>com.fasterxml.jackson.core</groupid>             <artifactid>jackson-databind</artifactid>             <version>2.6.5</version>         </dependency>         <dependency>             <groupid>com.fasterxml.jackson.core</groupid>             <artifactid>jackson-annotations</artifactid>             <version>2.6.5</version>         </dependency>           <!-- jersey dependency -->         <dependency>             <groupid>org.glassfish.jersey.bundles</groupid>             <artifactid>jaxrs-ri</artifactid>             <version>2.21.1</version>         </dependency>          <!-- make jersey auto marshel json -->         <dependency>             <groupid>org.glassfish.jersey.media</groupid>             <artifactid>jersey-media-json-jackson</artifactid>             <version>2.21.1</version>         </dependency>           <!-- make jersey auto marshel xml -->         <dependency>             <groupid>org.glassfish.jersey.media</groupid>             <artifactid>jersey-media-jaxb</artifactid>             <version>2.21.1</version>         </dependency>         <dependency>             <groupid>org.codehaus.woodstox</groupid>             <artifactid>stax2-api</artifactid>             <version>3.1.4</version>         </dependency>     </dependencies> 

my rest config

package com.blithe.rest;  import javax.ws.rs.*; import org.glassfish.jersey.server.*;  @applicationpath("/services") public class restserviceconfig extends resourceconfig {         public restserviceconfig(){              packages("com.blithe.rest","com.blithe.resource");         }  } 

a simple resource

package com.blithe.resource;  import java.util.arraylist; import java.util.list;  import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype;  @path("helloworld") public class helloworldresource {         @post     @produces(mediatype.application_json)     public list<string> getstringarray(){          list<string> list = new arraylist<>();         list.add("hello");         list.add("world");         return list;     }      } 

and rs client

package com.blithe.client;  import java.util.list;  import javax.ws.rs.client.client; import javax.ws.rs.client.clientbuilder; import javax.ws.rs.client.invocation.builder; import javax.ws.rs.client.webtarget; import javax.ws.rs.core.generictype; import javax.ws.rs.core.mediatype;  public class rsclient {      public static void main(string [] args){          client client = clientbuilder.newclient();          webtarget target = client.target("http://localhost:8080/restpom/services/helloworld");          builder builder = target.request(mediatype.application_json);          list<string> list  =builder.post(null , new generictype<list<string>>(){});               system.out.println(list.get(0)+list.get(1)); // out put helloworld         }     } 

run project on server publish restful service, , run main() method. should see helloworld on console.

what need is, make rs client in servlet. can refer rsclient above.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -