Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

how to invoke the static java calss when the method name mentioned in constructor

Hi Experts,

My Test class having

      hashMap.put("file", bytesArray);
      hashMap.put("filename", fname);
      hashMap.put("event", event);
      ObjectMapper objectMapper = new ObjectMapper();
      String requestBody = objectMapper.writeValueAsString(hashMap);      
      //url:http://10.0.9.1:8080/global/uploadAndGetDocID

Open in new window

my base uri is -
http://10.0.9.1:8080/global
method name is uploadAndGetDocID

Open in new window

i have constructor class

i want to call this method uploadAndGetDocID and from there
i want to invoke the postMultipartFormDataAsJson method and get the response.

this method not specified any where but mentioned in constructor as below
_uriBuilder = _uriBuilder.path("uploadAndGetDocID");

Static class: 

public static class UploadAndGetDocID {

            private com.sun.jersey.api.client.Client _client;
            private UriBuilder _uriBuilder;
            private Map<String, Object> _templateAndMatrixParameterValues;

            private UploadAndGetDocID(com.sun.jersey.api.client.Client client, UriBuilder uriBuilder, Map<String, Object> map) {
                _client = client;
                _uriBuilder = uriBuilder.clone();
                _templateAndMatrixParameterValues = map;
            }

           
            public UploadAndGetDocID(com.sun.jersey.api.client.Client client, URI baseUri) {
                _client = client;
                _uriBuilder = UriBuilder.fromUri(baseUri);
                _uriBuilder = _uriBuilder.path("uploadAndGetDocID");
                _templateAndMatrixParameterValues = new HashMap<String, Object>();
            }

         

            public<T >T postMultipartFormDataAsJson(Object input, Class<T> returnType) {
                UriBuilder localUriBuilder = _uriBuilder.clone();
                com.sun.jersey.api.client.WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));
                com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
                resourceBuilder = resourceBuilder.accept("application/json");
                resourceBuilder = resourceBuilder.type("multipart/form-data");
                com.sun.jersey.api.client.ClientResponse response;
                response = resourceBuilder.method("POST", com.sun.jersey.api.client.ClientResponse.class, input);
                return response.getEntity(returnType);
                
            }

        }

Open in new window


can some one suggest how to invoke it and get the response from postMultipartFormDataAsJson method




Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

my base uri is -
You need a Client as well as that to create an instance
Avatar of srikotesh
srikotesh

ASKER

hi
Can i create as below.

ClientConfig cc = new DefaultClientConfig();
 com.sun.jersey.api.client.Client createClientInstance = createClientInstance(cc);
private static com.sun.jersey.api.client.Client createClientInstance(ClientConfig cc) {
        return com.sun.jersey.api.client.Client.create(cc);
    }

Open in new window

      
Well you don't want to be doing all that kind of stuff outside a method, but that general approach should be OK.
A question: you're not by any chance working with API source code are you?
Hi cehj,

I am gettiing error while invoking the below code:

URI obj = null;
      try {
         obj = new URI("http://10.0.9.1:8080/global/uploadAndGetDocID");
      } catch (URISyntaxException e) {
         e.printStackTrace();
      }
       ClientConfig cc = new DefaultClientConfig();
        com.sun.jersey.api.client.Client createClientInstance = createClientInstance(cc);
        UploadAndGetDocID uploadDocId = new UploadAndGetDocID(createClientInstance, obj);
        String postMultipartFormDataAsJson = uploadDocId.postMultipartFormDataAsJson(hashMap, String.class);
       System.out.println("postMultipartFormDataAsJson "+postMultipartFormDataAsJson);

Open in new window


com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, multipart/form-data, was not found.

hi cehj,

i worked on api code the jersey api i didnt touched till now.
I don't know this API (i can't even find the link to the javadoc) but i would guess you either need to find an implemented writer or define a custom one

I'm also going to take a guess that it would be easier to do if you were to write a proper bean rather than using a Map. e.g.

public class FileData {
    private byte[] data;
    private String fileName;
    private String event;

    public FileData() {
    }

    public FileData(byte[] data, String fileName, String event) {
        this.data = data;
        this.fileName = fileName;
        this.event = event;
    }

    public byte[] getData() {
        return this.data;
    }

    public String getFileName() {
        return this.fileName;
    }

    public String getEvent() {
        return this.event;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String toString() {
        return String.format("%s=%s,%s=%s,%s=%s", "data", data, "fileName", fileName, "event", event);
    }
}

Open in new window

hi cehj,

now i have changed map to bean class while hitting the service getting 500 error.

response.getStatus()//getting 500 error.

Restful webservices API
i am consuming restful post method/get method using java
That suggests that the URI is wrong or the service doesn't exist
same requirement is availble in this link.

https://stackoverflow.com/questions/27609569/file-upload-along-with-other-object-in-jersey-restful-web-service 

file uploading service i am trying

Service is available i am able to hit from postman and getting the response.



You've got the service running on your own web server then?
Service is available i am able to hit from postman and getting the response.

I'm out of my depth now as i don't do REST
ok
But ... you've probably got the source of the service haven't you? If so, it would help to post it
Looking a little at the Jersey multipart implementation i see that your client is a long way off what is required (as was my guessed suggestion of using a bean)
In the distro, there's a test example in jersey/examples/multipart-webapp/src/test/java/org/glassfish/jersey/examples/multipart/webapp/MultiPartWebAppTest.java but with little or no commentary
Hi Cehj,

the static class which i shared in the above comment is the client source code-->UploadAndGetDocID.
OK but i'm not convinced it's up to date. Where did you get it from?

e.g. com.sun.jersey.api.client.WebResource no longer exists in the supported API:

https://eclipse-ee4j.github.io/jersey.github.io/apidocs/latest/jersey/index.html
I suspect that all the sun stuff is now superseded and unsupported
I have wadl uri-from that i have generated the client code with your help,in my previous question i have asked about
how to generate source code from wadl
There's some confusion then. Which API is being used here: Jersey, or something else?
Jersey API is used to generate client code.

Right. Unfortunately it seems to be badly documented, with examples virtually non-existent
i am also suspecting client generated code does not look like clean code.
Jersey API is used to generate client code.
Actually i don't think that's correct. I think it's Apache CXF
But i have another wadl service request call.

this 2nd service is using to send msg(this different wadl)
below code is working for it.i am getting the response as well.but here i  didnt used any client code.only with jerseyapi
i have called it is working.
ClientResponse response = webResource.accept("application/json")
                                 .header("Content-Type","multipart/form-data")
                                 .header("groupId","123")
                                 .type("application/json")
                                 .post(ClientResponse.class,request);

Open in new window


but for my first service i have to do the same way for file uploading.with out considering the staic class uploadgetdocid
with jersyapi can i call it in similar way?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Cehj,

the below code is working to call services
Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
         FileDataBodyPart filePart = new FileDataBodyPart("file", new File(fileUpload.getChannelFileName()));
         multipart = (FormDataMultiPart) formDataMultiPart.field("file", "key-file")
                     .field("filename", fileUpload.getChannelFileName())
                     .field("event", fileUploadRequest)
                     .bodyPart(filePart);

Open in new window

thanks
Glad you got it working