Open ywlcn opened 5 years ago
public class CustomerHandlerMethodArgumentResolver implement HandlerMethodArgumentResolver{
// 支持的类型判断用
@Override
public boolean supportsParameter(MethodParameter parameter) {
return UserData.class.isAssignableFrom(parameter.getParameterType());
}
// Request参数绑定
@Override
public Object resolveArgument(MethodParameter parameter
, ModelAndViewContainer container
, NativeWebRequest request
, WebDataBinderFactory factory) throws Exception {
///////
return userData;
}
}
使用例子
public class RequestHeaderModelAttributeMethodProcessor extends ModelAttributeMethodProcessor {
// 构造函数
public RequestHeaderModelAttributeMethodProcessor() {
super(true);
}
// 支持的类型判断用
@Override
public boolean supportsParameter(MethodParameter parameter) {
return RequestHeader.class.isAssignableFrom(parameter.getParameterType());
}
// Request参数绑定
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("userAgent", request.getHeader("user-agent"));
pvs.addPropertyValue("accept", request.getHeader("accept"));
pvs.addPropertyValue("acceptLanguage", request.getHeader("accept-language"));
pvs.addPropertyValue("acceptEncoding", request.getHeader("accept-encoding"));
binder.bind(pvs);
}
}
@PathVariable /api/get/1のように、REST形式のURIからパラメータを取るのに利用します。
@MetrixVariable PathVariable类似? 默认设置的情况下不能使用 需要修改配置?
@RequestParamアノテーションを指定すると、URLに含まれるクエリパラメータや、メッセージボディーに含まれるポストパラメータを受け取ることができます。
@RequestHeader リクエストヘッダを受け取ります。
@CookieValue クッキーの値を受け取ります。
@Requestbodyリクエストのボディを受け取ることができます。
@RequestAttribute はHttpServletRequestオブジェクトから取得される
@Value
@ModelAttribute
@SessionAttribute 从HttpSession取得的情报
@RequestAttribute 从HttpServletRequest取得的情报
JSP EL式 可以使用的 默认 结构体
编号 | 例外 | HTTP Status Code |
---|---|---|
(1) | org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException | 404 |
(2) | org.springframework.web.HttpRequestMethodNotSupportedException | 405 |
(3) | org.springframework.web.HttpMediaTypeNotSupportedException | 415 |
(4) | org.springframework.web.HttpMediaTypeNotAcceptableException | 406 |
(5) | org.springframework.web.bind.MissingPathVariableException | 500 |
(6) | org.springframework.web.bind.MissingServletRequestParameterException | 400 |
(7) | org.springframework.web.bind.ServletRequestBindingException | 400 |
(8) | org.springframework.beans.ConversionNotSupportedException | 500 |
(9) | org.springframework.beans.TypeMismatchException | 400 |
(10) | org.springframework.http.converter.HttpMessageNotReadableException | 400 |
(11) | org.springframework.http.converter.HttpMessageNotWritableException | 500 |
(12) | org.springframework.web.bind.MethodArgumentNotValidException | 400 |
(13) | org.springframework.web.multipart.support.MissingServletRequestPartException | 400 |
(14) | org.springframework.validation.BindException | 400 |
(15) | org.springframework.web.servlet.NoHandlerFoundException | 404 |
@ControllerAdvice
public class ExceptionHandler {
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@org.springframework.web.bind.annotation.ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
@ResponseBody
public Map<String, Object> handleError() {
Map<String, Object> errorMap = new HashMap<String, Object>();
errorMap.put("message", "Error");
errorMap.put("status", HttpStatus.METHOD_NOT_ALLOWED);
return errorMap;
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handlerException() {
.......
return “error/systemError”;
}
}
項番 | 类名 | 变换 | 支持的MediaType | 支持的JavaType |
---|---|---|---|---|
(1) | org.springframework.http.converter. ByteArrayHttpMessageConverter | HTTPBody(Text文本 or 2进制数据)⇔Byte数组 | / | byte[] |
(2) | org.springframework.http.converter. StringHttpMessageConverter | HTTPBody(Text文本)⇔文字列 | text / * | String |
(3) | org.springframework.http.converter. ResourceHttpMessageConverter | HTTPBody(2进制数据)⇔Spring的ResourceObject | / | Resource |
(4) | org.springframework.http.converter.xml. SourceHttpMessageConverter | HTTPBody(XML)⇔XMLObject | ・text/xml ・application/xml ・application/*-xml |
Source |
(5) | org.springframework.http.converter.support. AllEncompassingFormHttpMessageConverter | HTTPBody⇔MultiValueMap | ・application/multipart/form-data :MultiValueMap<String, Object> ・application/x-www-form-urlencoded :MultiValueMap<String, String> |
Map<K, List<?>> |
項番 | クラス名 | 变换 | MediType | 需要的库(存在于ClassPath) | サポート型 |
---|---|---|---|---|---|
(6) | org.springframework.http.converter.feed. AtomFeedHttpMessageConverter | HTTPBody(Atom)⇔AtomObject | application/atom+xml | ROME | Feed |
(7) | org.springframework.http.converter.feed. RssChannelHttpMessageConverter | HTTPBody(RSS)⇔RssObject | application/rss+xml | ROME | Channel |
(8) | org.springframework.http.converter.json. MappingJackson2HttpMessageConverter | HTTPBody(JSON)⇔JavaBean | application/json,application/*+json | Jackson2 | Object (JavaBean) Map |
(9) | org.springframework.http.converter.xml. MappingJackson2XmlHttpMessageConverter | HTTPBody(XML)⇔JavaBean | text/xml,application/xml,application/*-xml | Jackson-dataformat-xml | Object (JavaBean) Map |
(10) | org.springframework.http.converter.xml. Jaxb2RootElementHttpMessageConverter | HTTPBody(XML)⇔JavaBean | text/xml,application/xml,application/*-xml | JAXB | Object (JavaBean) |
(11) | org.springframework.http.converter.json. GsonHttpMessageConverter | HTTPBody(JSON)⇔JavaBean | application/json,application/*+json | Gson | Object (JavaBean) Map |
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setWriteAcceptCharset(false);
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringConverter);
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jackson2XmlPresent) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().applicationContext(this.applicationContext).build();
messageConverters.add(new MappingJackson2XmlHttpMessageConverter(objectMapper));
}
else if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
else if (gsonPresent) {
messageConverters.add(new GsonHttpMessageConverter());
}
}
Web.xml
缓存控制:
response.setHeader("Cache-Control", "no-cache");
return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(20, TimeUnit.SECONDS)) .body(body);