Im new to java based web service development.
I need to create a web service which accepts multipart data(ex: zip file).
Please help me out how to mention that in the function.
below is my current web service code which is accepting data in the form of json.
#RequestMapping(value="/workitems/updateData", method=RequestMethod.POST)
#ResponseBody
public Object updateData(#RequestHeader String deviceToken, #RequestBody FormFields[]
formFields,HttpServletResponse response) throws Exception {
//some code
}
please guide me to how to accept the multipart data in the web service method.
thanks in advance.
#RequestMapping(
value ="/workitems/updateData",method=RequestMethod.POST ,headers="Accept=application/xml, application/json")
public #ResponseBody
Object updateData(HttpServletResponse response,#RequestHeader String deviceToken,
#RequestParam ("file") MultipartFile file) throws Exception {
}
You can support it as above.
You can use normal Upload technique which you use in Servlet - commons-fileupload.jar way.
The same code placed in a method inside your controller will work fine. Make sure you pass HttpServletRequest object to your method.
Related
How to Consume a Json Request,Coming from some other Application like ".Net" and i want to Consume that into my Java Application .
How to Consume this with Controller in Spring MVC .
Thanks
Shashank
If I'am able to understand your question then you are asking about how to Post JsonRequest to RestController, for that I'm attaching a code snippet and hope it helps.
Step1: Create a Model Class of that JSON Request.
Step2: Mark #RequestBody Annotation with Controller method to get that type of Object in Method argument.
#RequestMapping(value = "/getRequest", method = { RequestMethod.POST },
produces = {"application/json"})
public #ResponseBody Object getResponse(#RequestBody JsonRequest request) {
sysout("Json Body: "+request.toString());
}
you are basically asking how controllers work !! a controller's job is to handle any (JSON or ...) request to its services.
I suggest you read some articles about spring MVC and controllers to understand how it works.
https://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration
https://www.in28minutes.com/spring-mvc-tutorial-for-beginners
I am using Java and spring boot and I need to get information by a URL and store it in my class in Java. My URL is like this one
localhost:8080/send?adress=example
I want to store the value of the URL parameter to a variable in Java.
I would suggest reading tutorials on spring boot and web services such as this one:
https://spring.io/guides/gs/rest-service/
The RequestParam annotation is used to bind method parameters to web request parameters. e.g.
#RequestMapping(value="/send", method=RequestMethod.GET)
public String method(#RequestParam(value="address") String address) {
...
}
You don't need to send a parameter with the URL, it should be sent through request parameters.
Once you have added a parameter with the request, in spring-boot by #RequestParam we can get parameter values. e.g
#RequestMapping(value="/order/search", method=RequestMethod.GET)
public String method(#RequestParam(value="orderNumber") String orderNumber) {
get Order by orderNumber...
}
Can I insert some includes in a spring mvc controller to point to various html files to assemble them as one page, kind of like (for example):
#RequestMapping(value = QUESTION_GROUP_CREATE_URL, method = RequestMethod.POST)
public
#ResponseBody
String createQuestionGroup(#RequestBody JsonQuestionGroup questionGroup, HttpServletResponse response) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
return "<div></div>";
}
but where I'm returning "<div></div>" can I use includes to link to head.html, header.html, nav.html, view.html, footer.html? Example?
Thank you!
You can use apache tiles instead of that ,It is very easy You can find many tutorial in web this is an example
http://www.codingpedia.org/ama/spring-mvc-and-apache-tiles-integration-example/
another one
https://bikashshaw.wordpress.com/2013/05/13/spring-mvc-create-jspjstl-composite-view-header-body-and-footer/
I am having an issue with an Ajax post to a RESTful web service in Java. The project utilizes a single servlet mvc model, with the Ajax post data being sent as JSON to the web service. The specific issue that is occuring is that I a unable to pull the data out of a HttpServletRequest object on the web service side. The POST goes directly to the web service, and I attempted to pull the data out with the following:
#Path(Ajax)
public AjaxResource(){
#Context
HttpServletRequest request;
#POST
#Produces("application/json")
#Consumes("application/json")
public Response postMethod(){
BufferedReader reader = request.getReader();
// additional code
}
}
I receive an IllegalStateException on the getReader() call on the request; from what I understand the input stream/reader can only be called once. I am unsure if this is due to the doPost method in the servlet doing a request.getParameter call as it seems to ago I'd hitting the servlet before this web service. Is there any other way to retrieve this data other than implementing HttpServletRequestWrapper in the servlet?
You should use #Context HttpServletRequest request as an argument of the resource method.
So it should be something like this:
public Response postMethod(#Context HttpServletRequest request){
// rest of the code
}
I have a JAX-RS service that I want to know what is the application context path. Before, I was using a HttpServletRequest to get the application url, using these methods:
public static String getApplicationBaseUrl(HttpServletRequest request) {
return String.format("%s://%s:%s%s",
request.getScheme(),
request.getServerName(),
request.getServerPort(),
request.getContextPath());
}
I saw the #Context in JAX-RS, but it returns only the paths beginning from JAX resource. What can I do for retrieve the application path?
I found the answer. I was using the #Context for javax.ws.rs.core.UriInfo class, but, I can inject the HttpServletRequest too. So, now I can use the getApplicationBaseUrl method that I wrote.