Spring MVC - get address from request - http

This is my case, set by step:
Run below page under address:
localhost:8080/user/data/expenses
On this page click link
localhost:8080/user/data/change_button/9_2015
Now my controller is started:
My question:
How can I get information from what side was call my controller. I need exactly this information which is strong below:
localhost:8080 /user/data/expenses
M-Z

Infact there is way to get the referer of the request as it is part of the HTTP header. Just inject HttpServletRequest to the mapped method then get the header named "referer".
request.getHeader("referer");

You need to inject the HttpServletRequest and then use getRequestURI() to get the data. Read the javadoc for #RequestMapping.

Related

How to get Http header values in Apache Camel- Jersey Rest API

I have an application which uses Apache Camel to build an API. It basically uses blueprint.xml to define routes and processing is done by a bean(please note its not any processor bean. Just a plain Java bean). It uses Jersey client to invoke the backend system Rest API.
My requirement is to get the http headers in the code to be able to send them to our custom logging system.
a) I tried #httpHeaders annotation but this does not inject the headers on my code.
b) Since its not using any BeanProcessor i dont have an Exchange object from where i can get the header values.
Please help with a way to get header values on the code.
Add the request context to your class
#Context
private HttpServletRequest request;
and get the headers in your endpoint using request.getHeader
Returns the value of the specified request header as a String.

Spring annotation for HTTP method

Is there any spring annotation available for capturing http method.
essentially I have a legacy controller where a single method is handling POST, PUT, PATCH.
Now I need to add a funcitonality which is applicable only for POST. Thus i want to throw an error if consumer sends request with method other than POST.
Thus I want to capture the request method.
I know I can get it easily using httpservletrequest.getMethod() but I want to use spring's annotation to capture it. This will be help me keep my code clean.
Query: Does spring support any annotation through which I can capture http the request method.
Thanks,
You have:
#GetMapping
#PostMapping
#PutMapping
(...)
and others
When you put for example #GetMapping and try to request another method you will automatically receive HTTP error 405 - method not allowed.
Available since version 4.3.

How to wrap request and response to generate customized request and response using servlet filters?

How to wrap request and response to generate customized request and response using servlet filters?
I want to write a filter that will be invoked before the request reaches to the servlet and want to do some modification in the request and then want to forward to the servlet.
Similarly, I want to modify the servlet generated response using the filter and want to forward the customized response to the client.
You would need to extend HttpServletRequestWrapper and HttpServletResponseWrapper and override respective methods. Please refer to below links for examples:
Modify request parameter with servlet filter
http://www.java2s.com/Tutorial/Java/0400__Servlet/Filterthatusesaresponsewrappertoconvertalloutputtouppercase.htm
Hope this helps!

Url Not Working in My Webservice

I am calling webservice which takes two parameters like category and salt then provide the json output with constructed url but url is not working. PFA url
Service Url:
http://qalisting.corelogic.com/ChaseListingServices/v1.5/test
constructed URL: http://qalisting.corelogic.com/ChaseListingServices/v1.5/listings/search/test/0/4e9c00b32794edfeba257aa0c74f500b
Looking at the error message on your constructed URL, the Service needs to be called using a POST and not a GET Request.
Responding to your comments: it's not the URL which is the problem but how it is called. When you follow a link, such as the one you posted - the browser sends it as a GET request. The service is expecting the JSON arguments as part of the "body" of a POST request. This must be done programmatically.

Access Response Headers from ASP.Net PageMethod Call

When using ASP.Net Ajax to call PageMethods, how can I access the Http response headers from the "success" method?
For example:
PageMethods.DoSomething(
function(result){successMethod(result)},
function(error){errorMethod(error)}
);
function successMethod(result){
//------how can I access the Http response headers from here? ------
}
Thanks for any help
In your example, PageMethods.DoSomething should have a return value equal to WebRequest if it's an asp.net web service proxy. This is provided so that you can manipulate the request after you've initiated it (i.e. cancel it etc).
With this class you have an add_completed method which you can use to add a handler for when the web request completes. The signature for the callback is function OnWebRequestCompleted(executor, eventArgs), and the executor parameter in this enables you to get hold of extra response information. For example, you can get hold of the response headers with executor.getAllResponseHeaders(); which should be a map (named collection) of header names and values.
So if you add a handler to the web request's completed event immediately after making the service method call, it should work (there's no web service in the world that can respond faster than two consecutive lines of code!).
The previous hyperlink to WebRequest contains a full example of how wire this up. Notice, however, that this code uses the WebRequest directly.
Asp.Net Ajax Web Service proxy classes use the WebServiceProxy class, and each proxy method ultimately call its invoke method, which returns the WebRequest instance.
A web request has a headers collection
http://msdn.microsoft.com/en-us/library/bb383774.aspx
The webrequestmanager is a static object that you may be able to extract this information from:
http://msdn.microsoft.com/en-us/library/bb397435.aspx
Hopefully, between the two links, it makes sense :-;
I'm not saying recode to use this necessarily, but page methods is a wrapper and as such I think it would access information from a web request, which can be affected from the WebRequestManager...

Resources