Uploading jar to nexus with Spring RestTemplate -


i use spring's resttemplate upload jar nexus. found instructions on how using curl here, works fine. have utterly failed in converting resttemplate call. here code:

    string auth = "admin:admin123";     byte[] encodedauth = base64.encodebase64( auth.getbytes(charset.forname("us-ascii")) );     string authheader = "basic " + new string( encodedauth );      resttemplate resttemplate = new resttemplate();      linkedmultivaluemap<string, object> files = new linkedmultivaluemap<>();     files.add("r", "releases");     files.add("haspom", "false");     files.add("e", "jar");     files.add("g", "com.test.proj");     files.add("a", "my-artifact");     files.add("v", "1.0.0");     files.add("p", "jar");     files.add("file", new bytearrayresource(jarbytes));      httpheaders headers = new httpheaders();     headers.setcontenttype(mediatype.multipart_form_data);     headers.set("authorization", authheader);     httpentity<linkedmultivaluemap<string, object>> entity = new httpentity<>(files, headers);     string response = resttemplate.postforobject("http://localhost:8081/nexus/service/local/artifact/maven/content", entity, string.class); 

this fails 400 bad request. after looking @ the source, looks file failing key check:

 (fileitem fi : files) {     if (fi.isformfield()) {       // parameters first in "nibble"       processformfield(request, uploadcontext, fi);     }     else {       // file, means no parameters income anymore       // either received gavs params, or have pom work (file1)       ... 

fileitem.isformfield apache commons fileupload. does know how succeed "file" passing in?

in question (fileupload isformfield() returning true when submitting file) answer suggests need name field, or perhaps in case, "type" doesn't come through. if case, possible specify these while making post request?

i'm thinking isn't possible resttemplate. opted use apache's httpcomponents, has worked wonderfully. here resulting code:

public void uploadtonexus(string version, file myjar, string artifactid) {     try(closeablehttpclient httpclient = httpclients.createdefault())     {         httppost httppost = new httppost("http://admin:admin123@mydomain:8081/nexus/service/local/artifact/maven/content");          filebody jarfilebody = new filebody(myjar);          httpentity requestentity = multipartentitybuilder.create()                 .addpart("r", new stringbody("releases", contenttype.text_plain))                 .addpart("haspom", new stringbody("false", contenttype.text_plain))                 .addpart("e", new stringbody("jar", contenttype.text_plain))                 .addpart("g", new stringbody("com.domain.my", contenttype.text_plain))                 .addpart("a", new stringbody("my-artifactid", contenttype.text_plain))                 .addpart("v", new stringbody(version, contenttype.text_plain))                 .addpart("p", new stringbody("jar", contenttype.text_plain))                 .addpart("file", jarfilebody)                 .build();          httppost.setentity(requestentity);          try(closeablehttpresponse response = httpclient.execute(httppost))         {             logger.info("response nexus: {}", response.tostring());         }     }     catch (ioexception e)     {         throw new runtimeexception("unable close httpclient", e);     } } 

i needed following maven dependencies:

    <dependency>         <groupid>org.apache.httpcomponents</groupid>         <artifactid>httpclient</artifactid>         <version>4.4.1</version>     </dependency>     <dependency>         <groupid>org.apache.httpcomponents</groupid>         <artifactid>httpmime</artifactid>         <version>4.4.1</version>     </dependency> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -