[springmvc]Injecting standard objects

1. HttpServletRequest (or its more portable WebRequest wrapper)、Principal、Locale

@RequestMapping(value="/data/standard/request", method=RequestMethod.GET)
public @ResponseBody String standardRequestArgs(HttpServletRequest request, Principal user, Locale locale) {
StringBuilder buffer = new StringBuilder();
buffer.append("request = ").append(request).append(", ");
buffer.append("userPrincipal = ").append(user).append(", ");
buffer.append("requestLocale = ").append(locale);
return buffer.toString();
}

2. Reader

@RequestMapping(value="/data/standard/request/reader", method=RequestMethod.POST)
public @ResponseBody String requestReader(Reader requestBodyReader) throws IOException {
return "Read char request body = " + FileCopyUtils.copyToString(requestBodyReader);
}

3. InputStream

@RequestMapping(value="/data/standard/request/is", method=RequestMethod.POST)
public @ResponseBody String requestReader(InputStream requestBodyIs) throws IOException {
return "Read binary request body = " + new String(FileCopyUtils.copyToByteArray(requestBodyIs));
}

4. HttpServletResponse

@RequestMapping("/data/standard/response")
public @ResponseBody String response(HttpServletResponse response) {
return "response = " + response;
}

5. Writer

@RequestMapping("/data/standard/response/writer")
public void availableStandardResponseArguments(Writer responseWriter) throws IOException {
responseWriter.write("Wrote char response using Writer");
}

6. OutputStream

@RequestMapping("/data/standard/response/os")
public void availableStandardResponseArguments(OutputStream os) throws IOException {
os.write("Wrote binary response using OutputStream".getBytes());
}

7. HttpSession

    @RequestMapping("/data/standard/session")
public @ResponseBody String session(HttpSession session) {
StringBuilder buffer = new StringBuilder();
buffer.append("session=").append(session);
return buffer.toString();
}

8. custom object injector:

  1) Implement the WebArgumentResolver extension point

  2) Register with the AnnotationMethodHandlerAdapter

public interface WebArgumentResolver {
Object resolveArgument(MethodParameter param, NativeWebRequest request);
}

RequestAttribute.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestAttribute {
String value();
}

CustomArgumentResolver.java

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class CustomArgumentResolver implements HandlerMethodArgumentResolver {

public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(RequestAttribute.class) != null;
}

public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
RequestAttribute attr = parameter.getParameterAnnotation(RequestAttribute.class);
return webRequest.getAttribute(attr.value(), WebRequest.SCOPE_REQUEST);
}

}

CustomArgumentController.java

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CustomArgumentController {

@ModelAttribute
void beforeInvokingHandlerMethod(HttpServletRequest request) {
request.setAttribute("foo", "bar");
}

@RequestMapping(value="/data/custom", method=RequestMethod.GET)
public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
return "Got 'foo' request attribute value '" + foo + "'";
}

}














原文地址:https://www.cnblogs.com/lavieenrose/p/2418731.html