Spring annotation for HTTP method - spring-mvc

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.

Related

Grpc Writing Interceptors for Specific Endpoint

I implemented a grpc server in Golang. This server has multiple endpoints, but for one of the endpoints I want to implement an interceptor that will check the validity of an authentication token before proceeding with the request. I know how to implement an interceptor that will run when a request reaches any of the grpc endpoints, but how can I make so that my interceptor only runs for one specific endpoint?
For those interested, I was able to find the method of the request by inspecting the grpc.UnaryServerInfo param of the interceptor. There is an attribute called FullMethod that gives you the the endpoint of the request.
You can get the method name and compare when you are getting a request through your interceptor. Your custom interceptor will have one component called ServerInfo which will help you to filter from which method you are getting called. Based on that, you can filter out your authentication endpoint

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 MVC - get address from request

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.

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!

Why sitebricks response with 405 status when using delete method?

I am implementing a simple rest service with the 4 http methods get,post, put and delete using sitebricks. trying to send a delete request to the defined service with a WebClient I get a 405 response. Does anyone knows why would I get such response ?
10:22:24.840 [5907955#qtp-6711891-2 - /r/clients/123] DEBUG org.mortbay.log - RESPONSE /r/clients/123 405
This is how I use web client
WebClient client = web().clientOf(delete(123)).transports(String.class).over(Json.class);
client.delete();
here is my delete method
#Delete
#At("/:id")
public Reply delete(#Named("id") String id) {
clientsRepository.delete(id);
return Reply.saying().ok();
}
I am using Jetty Server.
Response code 405 means that something is configured to not allow the use of the http DELETE method.
I can't speak for sitebricks itself, but servlet spec allows you to disable specific methods.
The web.xml of your webapp or the ${jetty.home}/etc/webdefault.xml, as either could be configured to disallow the use of specific HTTP methods (like TRACE, PUT, DELETE).
Check those files for <security-constraints> that might have <http-method> declarations for DELETE.
Also note that any code can trigger the 405 response itself.
Since you are seeing this in Sitebricks, possibly a <filter> in your web.xml is preventing it.
Are you constructing the request URI properly? Sitebricks returns 405 if there is no handler method. This test case verifies that #Delete does indeed work properly:
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/java/com/google/sitebricks/example/RestfulWebServiceWithSubpaths2.java
Also as joakime says, do check if any other filters or handlers are firing outside Sitebricks.

Resources