Adding HTTP Headers in JSP - spring

I am creating a Spring REST web services, that communicates with Android App and JSP web pages.
The method at my spring controller is like
#RequestMapping(method = RequestMethod.POST, value = "/login")
public ModelAndView userLogin(#RequestBody User user,
HttpServletRequest request){
//do something with user
}
Andoid App is able to access this method through adding request Headres like
"Content-Type" application/json , "Accept" application/json etc. Here the user information sent by android end is comes in request body. Thats ok..
But problem occurs when i POST the contents from my JSP page. I am not able to access the same userLogin method from jsp page with #RequestBody but when i replace it with #ModelAttribute it works for jsp page ...but then doesn't works for android app. Please tell me how can i solve this.

Make the JSP page do the same thing as the android app (posting as JSON) using JavaScript, or implement a second method in your Spring controller (userLogin2), mapped to a different URL, and use this URL in your JSP.

Related

Spring cloud Feign:no suitable HttpMessageConverter found for response type [class org.springframework.web.servlet.ModelAndView]

I have a service which has a url that return ModelAndView Object.
In its own point, I can access the website. But when I use spring cloud feign to invoke that url, it comes out that no suitable HttpMessageConverter found for response type [class org.springframework.web.servlet.ModelAndView] and contentType text/html. Here is my feign client.
Please try to change empList() method in your ConsumerController class like below.
public String empList() {
return empService.empList();
}
ModelAndView is not the actual response of /emplist from EmpController. It will be handled by DispatchServlet and ViewResolver will resolve the actual view with your view name - emp. So, from the view of ConsumerController, The response will be String object.
Anyway, I'm not sure that it's a good idea that accessing another web page via feign client in your case. Because if the original html page contains additional resources like images that existing in your origin server, it will not be served.

How to access previous SOAP headers in SoapInterceptor?

Here's my configuration:
A request is captured by my REST controller, I send data via SOAP to my webservice. Then I access some data sending SOAP request to another service and I return gathered data to the user sending the request.
Before sending SOAP request to external webservice I need to set some headers, so I have an interceptor that extends AbstractSoapInterceptor with Phase.PRE_PROTOCOL in constructor by webservice side.
Inside handleMessage() I create new headers and add to SoapMessage but... the data I need to set is inside REST request inside it's headers. So in order to get them I need to have access to HttpServletRequest and then I just get the header using HttpServletRequest#getHeader("header_name").
I've saw here I could just use #Context annotation but it's not available in my Spring (3.0.5) or maybe RESTEasy is something else and that question isn't connected with mine in anyway
I've tried this:
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
and
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
but it's been a shot in the dark and it failed.
Edit:
Thanks to Abel ANEIROS I've added:
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
and was able to get HttpServletRequest from RequestContextHolder.getRequestAttributes() but apperently it's origin is from my webservice and not REST
Edit2:
The structure is different than I thought.
It's: Rest Controller -> MyWebService (SOAP) -> ExternalService (SOAP)
I've created another interceptor in controller side, added headers to SOAP message and now I'm trying to get the headers again in MyWebService side.

Would someone answer some questions about Spring and Request Mapping?

I just started studying Spring, and I'm so confused.
I just created a new 'Spring Legacy Project' at STS. HomeController and home.jsp are there.
When I run it on server, it comes through the HomeController first, and arrives to home.jsp.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
What makes my project go through the HomeController at the beginning? Should I look at any xml file?
What does value="/" mean in #RequestMapping?
At the home.jsp, I made a button to go 'result.jsp'. From the 'result.jsp' I want to go back to home.jsp. but it doesn't work. What should I do?
<input type="button" value="뒤로 " onclick="javascript:location.href='/views/home.jsp'">
Why isn't this button working? Those two JSP files are in same place.
Your app is deployed to some app server, such as tomcat. The request <app server>/<context root> is handled by the app server to the .war with the appropriate context root, e.g. to your app. Your app uses Spring MVC, so it is Spring's RequestMappingHandlerMapping bean from your .war file that initially handles the request and finds your method that will handle this request. It does so by comparing the path in the request with the value of each method annotated with #RequestMapping.
The annotation #RequestMapping(value="/") of your home() method means that request <app server>/<context root> will be handled by your 'home()' method.
Any request from your JSP will go back to the Spring MVC that will try to map it to the appropriate controller method, i.e. to the method annotated by #RequestMapping with the appropriate path relative to the context root. So if your result.jsp just links to "/", it should bring you to the home() method and then to the home.jsp.

How to send the send status code as response for 404 instead of 404.jsp as a html reponse in spring?

I created web application in spring and handled exception mappings for 404 and 500.If system doesn't find any resources for requested URL it redirects into custom 404.jsp instead of regular 404 page.Everything works fine till have decided to add a webservice in my app.I have included one more controller as a webservice there is no view for this controller and this needs to be invoke through curl command.
User may get into change the curl script.If they changed the URL it should show 404 status code.But it returns the custom 404.jsp as a html response instead of status code.Because dispatcher servlet will takes all urls with /*.
How I can solve this issue?
Please share your suggestions.
Spring 3.2 introduced the #ControllerAdvice, and as mentioned in the documentation:
It is typically used to define #ExceptionHandler
That means you can use the #ControllerAdvice to assist your #Controller like the following:
#ControllerAdvice
class GlobalControllerExceptionHandler {
#ResponseStatus(HttpStatus.NOT_FOUND) // 404
#ExceptionHandler(Exception.class)
public void handleNoTFound() {
// Nothing to do
}
}
For further details please refer to this tutorial and this answer.

difference between Rest web service method call and non Rest web service method call or normal method call

Can anyone please let me know the difference between rest web service method call and normal method call.
I developed a website, in which I invoked a method in controller using the following way,
#RequestMapping("/something.do")
and now I changed the same method to web service. Now I invoke the same method like
#RequestMapping(method=RequestMethod.GET, value="/something.do",headers="Accept=application/json")
And the URL i used in the AJAX call before and after converting to web service is "something.do". Everything is working fine.
My question is, if it is Rest web service then my URL should be something like
locahost/ProjectDisplayName/something.do. But it is working fine even when I make ajax call with something.do in URL. If this is correct what is difference between them?
Or please correct me where I am wrong.
See when you make rest call, and intercept it using controller.
Then you can send different parameters along with the call.
You can format your url before call restTemplate.executeService(Object ... obj)
so that it will map to below method.
eg.
<code>
#RequestMapping(value = "/something/{id}", method = RequestMethod.GET)
#ResponseStatus(HttpStatus.OK)
public #ResponseBody
testJsonMessage getDataById(
#PathVariable("id") String id) {
}
</code>

Resources