Is there a way to use the value of the annotation inside the same method that it has been declared ?
#GET
#Produces({MediaType.APPLICATION_XML})
#Path(CONSTANTS.PATH1)
public MyModel getInfo(
#PathParam(CONSTANTS.ID) String id,
#Context HttpServletRequest request,
#Context HttpServletResponse response) {
...
}
In the above example, is it possible to use the value of #Path(CONSTANTS.PATH1) inside the method? I can directly use the value of CONSTANTS.PATH1, but if it possible to get it from annotations itself ?
Related
This question already has answers here:
Spring MVC: bind request attribute to controller method parameter
(7 answers)
Closed 7 years ago.
I have a HandlerInterceptorAdaptor.preHandle() method that simplified looks like this:
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
request.setAttribute("MyObject", myObject);
return true;
}
Next when my #RestController gets called, I would like it to look like this:
#RequestMapping(value="/", method=RequestMethod.PUT)
public ResponseEntity myMethod (MyObject myObject) {
}
I imagine there is some annotation I can put there where Spring will add the attribute I set earlier in the HandlerInterceptorAdaptor.
Could someone please tell me what that is?
Why not like this?
#RequestMapping(value="/", method=RequestMethod.PUT)
public ResponseEntity myMethod (HttpServletRequest request, HttpServletResponse response) {
MyClass obj = (MyClass) request.getAttribute("myObject");
}
I have the next method:
#RequestMapping(value="/busqueda/basica", method = {RequestMethod.POST,RequestMethod.GET})
public String busquedaBasica(HttpServletRequest request,
HttpServletResponse response,
ModelMap modelMap,
#RequestParam("nombreBasica") String nombre){
...
}
Is there any way to get the method's request, POST or GET?
Yes, the HttpServletRequest has a getMethod() that returns a String value representing the HTTP method.
I'm new to Jersey, and want to determine the #Produces type in other contexts, so I can use it during error handling cases.
For example, I have the following method that produces json:
#Path("test-json")
#Produces(MediaType.APPLICATION_JSON)
#GET
public Object getTestJson(#Context HttpServletRequest req, #Context HttpServletResponse res) throws Exception
{
throw new RuntimeException("POST submitted without CSRF token! ");
}
Later on, in a global exception handler, I'd like to get the #Produces media type.
I've tried doing this with something like the following, but getMediaType() is returning null (note that this is simplified, but headers is not null in all of my tests, just getMediaType() is null).
public class someClass
{
#Context
HttpHeaders headers;
public Response convertExceptionToResponse(T exception)
{
MediaType mediaType = headers.getMediaType();
// At this point, I thought media type would be
// MediaType.APPLICATION_JSON
// for the above 'getTestJson' method, but it's null.
}
}
How can I do this?
JAX-RS
Inject ResourceInfo and invoke getResourceMethod() which will return Java Method. Then you can simple retrieve declared annotations. The problem here is that with this approach you need to do a lot of coding in case #Produces is not located directly on a method but somewhere in the hierarchy.
Jersey 2
Inject ExtendedUriInfo
#Context
private ExtendedUriInfo uriInfo;
and look for matched ResourceMethod (getMatchedResourceMethod()). Then simply get list of producible media types (getProducedTypes()).
I have a method which returns true or false based on some parameters. So I make an ajax call (using Ext.ajax.request). In spring 2.x version how do I send back the result?
So for my controller I extend BaseSimpleCommandController and override the method
ModelAndView doExecute(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
I want to know what would i need to do to send back just a boolean value. I am little confused as what needs to be done. I now i have to send back a ModelAndView type object but nor sure i should i embed a single boolean value in to this object.
EDIT: the BaseSimpleCommandController is specific to my project which in turn extends AbstractCommandController from spring. sorry for the confusion.
If you just want to return "true" or "false" there is no need to use models, command controllers, etc. Simply inject HttpServletResponse and send the data directly:
public void handle(HttpServletResponse response) {
boolean flag = //...
response.getWriter().print(flag);
}
Request params can be mapped via annotation in the method parameter, or through the request's parameter map
Method Parameter:
#RequestMapping(method=RequestMethod.GET)
public void someCall(#RequestParam(value="param1") String paramName)
...
Where param1 would be the get parameter param1. If you don't provide a value for the annotation, it tries to bind to the name of the parameter in the method name (paramName in this case).
Parameter Map
#RequestMapping(method=RequestMethod.GET)
public void someCall(HttpServletRequest request)
{
Map<String, String[]> paramMap = request.getParameterMap();
}
Hope this helps!
I have the method below:
#RequestMapping(value = "/path/to/{iconId}", params="size={iconSize}", method = RequestMethod.GET)
public void webletIconData(#PathVariable String iconId, #PathVariable String iconSize, HttpServletResponse response) throws IOException {
// Implementation here
}
I know how to pass the variable "webletId" from the RequestMapping using the #PathVariable, but how do I reference the variable "iconSize" from params?
Thanks a lot.
Use #RequestParam:
#RequestMapping(value = "/path/to/{iconId}", method = RequestMethod.GET)
public void webletIconData(#PathVariable String iconId,
#RequestParam("size") String iconSize,
HttpServletResponse response) throws IOException { ... }
See also:
15.3.2.3 Supported handler method arguments and return types
axtavt is right
I only want to explain what your mistake is:
The #RequestMapping params parameter is a filter to make sure that the annotated handler method is only invoked if there is a parameter with the requested value.
So a handler method annotated with #RequestMapping(params="action=doSomething") will be only invoked if there is an request parameter actionwith the content doSomething.