I would like to know how to create a REST webservice that posts multipart request to upload the file without using third party libraries like Jersey or Spring.
Thanks.
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("/uploadfile")
void uploadFile(#Context HttpServletRequest request, #Multipart(value = "file", type = MediaType.APPLICATION_OCTET_STREAM)Attachment file);
Related
I am using Java with Apache CXF to write the backend for AngularJS (single-page web site). In my REST service, I need access to the header of the http request (i.e. parameters and cookies) and I need access to the response header also (i.e. parameters and cookies). The main reason for this is security, authentication purposes and session management. Those are important reasons.
Is there a way of getting both of these structures in Apach CXF RESTfull code?
You can inject request by using #Context [javax.ws.rs.core.Context]
public Response myRest(#Context HttpServletRequest request /*, other parameters if you have like #QueryParam */ ){
request.getCookies();
request.getUserPrincipal();
}
You can set cookie or header in response as bellow
ResponseBuilder builder = Response.ok(); //Response.status(500) either way
builder.cookie(arg0);
builder.header(arg0, arg1);
return bulider.build();
Using Coinbase, I need to design an interface for the callback.
My application uses JSF 2.2, and I do not really know how to intercept the Coinbase request.
Using servlet, I could retrieve the contents of the request in a doGet, but with JSF I'm stuck!
JSF framework itself is a servlet. Everything you need to get from request you can get in JSF through Faces API.
Example, to get the request object:
HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance()
.getExternalContext().getRequest();
and so on.
Otherwise you can always create a custom servlet and map it in web.xml next to Faces servlet.
Is there a way in CXF to implement a Restful webservice which will accept different xml requests using one Web service method?
e.g. Can one create a Restful endpoint to accept this type of XML through one web service method?
<Data>
<Book>BN1</Book>
</Data>
& this too using same web service method?
<Data>
<Disk>DN1</Disk>
</Data>
I think this post: Apache CXF: Consume XML POST payload... shows a good example of how to declare a CXF REST service as receiving POST XML data.
For your example of handling different XML content, instead of 'Bean' in the above you'd have an #XmlRootElement that's the Data, with a child that's a #XmlAnyElement.
Ok so I am using this for generic XML.
public interface Callback {
#POST
#Path("/submit")
#Consumes("text/xml")
#Produces("application/xml")
public Response submit(String incomingXML);
}
Basically I am getting whole xml as a string in my method body, As CXF is not parsing it, it can remain generic.
I'm trying to do a POST request from any of rest client like (Advanced rest client, Postman etc) for posting a request with mime type "multipart/related" but none of rest client supports. So is there a way to quickly POST a request from any of rest clients or other alternate solutions?
Though I have not tried multipart/related, but multipart/mixed using jersey client.
Using Jersey client
Create a multipart request .. eg.
MultiPart multiPart = new MultiPart();
multiPart.bodyPart(....//
Then call the service like :
Client c = Client.create();
WebResource service = c.resource(Url);
ClientResponse response = service.type("multipart/mixed").header(headerkey,
headervalue).post(ClientResponse.class, multiPart);
Similarly try it for multipart/related.
you have download jersery client jar from https://jersey.java.net/download.html
Attached image will help you, This way you can try using Advance Rest Client plugin in Chrome, and also don't forget to upload the file under the Files tab.
I want to write a RESTful web service for client to upload data.
The data format is JSON
But I don't know much about it,can you give some sample code in JAVA? include the code of service and client that could demonstrate me the whole process of data uploading.
A good place to start is the Jackson Tutorials. Then look at either Jersey's JSON Support or RESTeasy's JSON Support depending on which framework you happen to be using. Data uploading is a open-ended topic since there are a number of different ways that it can be accomplished. If you POST JSON directly to the service then you can use JAXRS annotations like:
#Path("/myservice")
public class MyService {
#POST #Consumes("application/mytype+json")
public Response processPostRequest (JsonBeanType postData) {
...
}
}
The processPostRequest method will be invoked whenever a client POSTs data that includes the Content-Type: application/mytype+json HTTP header to the /myservice resource.
Another way to upload data is to send it using an HTML form. There are a bunch of examples of processing HTML forms in Java. The SO question How can I handle multipart form data post requests in my Java servlet should start you off in the right direction.