|
| 1 | +package com.web.spring.databind; |
| 2 | + |
| 3 | +import com.web.spring.databind.domain.ResponseError; |
| 4 | +import com.web.spring.databind.domain.SimpleUser; |
| 5 | +import com.web.spring.databind.domain.ComplexUser; |
| 6 | +import java.text.SimpleDateFormat; |
| 7 | +import java.util.Date; |
| 8 | +import javax.validation.Valid; |
| 9 | +import org.springframework.beans.propertyeditors.CustomDateEditor; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.validation.BindingResult; |
| 15 | +import org.springframework.web.bind.WebDataBinder; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.InitBinder; |
| 18 | +import org.springframework.web.bind.annotation.PostMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestBody; |
| 20 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 21 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 22 | + |
| 23 | +/** |
| 24 | + * Created by Administrator on 2017/7/9. |
| 25 | + */ |
| 26 | +@Controller |
| 27 | +@RequestMapping(value = "/databind") |
| 28 | +public class DataBind { |
| 29 | + |
| 30 | + // 返回带有http status code的响应体 |
| 31 | + |
| 32 | + /** |
| 33 | + * TODO http://localhost:18080/databind/simpleObject?name=sun&age=27 |
| 34 | + * 简单对象映射成对象中的字段,字段从url中的参数中读取 |
| 35 | + */ |
| 36 | + @GetMapping(value = "/simpleObject", produces = "application/json") |
| 37 | + public ResponseEntity getSimpleObject(@Valid SimpleUser user, BindingResult result) { |
| 38 | + if (result.hasErrors()) { |
| 39 | + ResponseError error = new ResponseError(result.getFieldError().getDefaultMessage()); |
| 40 | + return new ResponseEntity(error, HttpStatus.BAD_REQUEST); |
| 41 | + } |
| 42 | + return new ResponseEntity(user, HttpStatus.OK); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * TODO http://localhost:18080/databind/simpleObject?name=sun&age=27&contactInfo.phone=8888888 |
| 47 | + * 复杂对象映射,成第一级对象的字段加第二级字段的{属性名.字段} |
| 48 | + */ |
| 49 | + @GetMapping(value = "complexObject") |
| 50 | + public ResponseEntity getComplexObject(@Valid ComplexUser user, BindingResult result) { |
| 51 | + if (result.hasErrors()) { |
| 52 | + ResponseError error = new ResponseError(result.getFieldError().getDefaultMessage()); |
| 53 | + return new ResponseEntity(error, HttpStatus.BAD_REQUEST); |
| 54 | + } |
| 55 | + return new ResponseEntity(user, HttpStatus.OK); |
| 56 | + } |
| 57 | + |
| 58 | + // 指定responseBody的返回格式 |
| 59 | + |
| 60 | + /** |
| 61 | + * TODO http://localhost:18080/databind/json?name=sunu&age=0015 |
| 62 | + * 返回json数据 |
| 63 | + */ |
| 64 | + @GetMapping(value = "/json", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 65 | + @ResponseBody |
| 66 | + public SimpleUser writeJson(@Valid SimpleUser user, BindingResult result) { |
| 67 | + return new SimpleUser(user.getName(), user.getAge()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * TODO http://localhost:18080/databind/json |
| 72 | + * 请求体, 解析json请求体 |
| 73 | + * { |
| 74 | + * "name": "sun", |
| 75 | + * "age": 15 |
| 76 | + * } |
| 77 | + */ |
| 78 | + @PostMapping(value = "/json", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 79 | + @ResponseBody |
| 80 | + public SimpleUser readJson(@Valid @RequestBody SimpleUser user, BindingResult result) { |
| 81 | + return new SimpleUser(user.getName(), user.getAge()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * 生成xml数据的模型, 需要使用注解去定义 |
| 86 | + */ |
| 87 | + @GetMapping(value = "/xml", produces = MediaType.APPLICATION_XML_VALUE) |
| 88 | + @ResponseBody |
| 89 | + public SimpleUser writeXml(@Valid SimpleUser user, BindingResult result) { |
| 90 | + return user; |
| 91 | + } |
| 92 | + |
| 93 | + @PostMapping(value = "/xml", produces = MediaType.APPLICATION_XML_VALUE) |
| 94 | + @ResponseBody |
| 95 | + public SimpleUser readXml(@Valid @RequestBody SimpleUser user, BindingResult result) { |
| 96 | + return user; |
| 97 | + } |
| 98 | + |
| 99 | + // 不指定responseBody的返回形式, 由客户端决定 |
| 100 | + |
| 101 | + /** |
| 102 | + * Ref: http://www.baeldung.com/spring-httpmessageconverter-rest |
| 103 | + * TODO http://localhost:18080/databind/typeByClient?name=sunu&age=0015 |
| 104 | + * TODO responseBody返回形式由client上报的header中的Accept决定 |
| 105 | + * Spring 4中会默认开启注解进行httpMessageConverter |
| 106 | + */ |
| 107 | + @GetMapping(value = "/typeByClient") |
| 108 | + @ResponseBody |
| 109 | + public SimpleUser writeByClient(@Valid SimpleUser user, BindingResult result) { |
| 110 | + return new SimpleUser(user.getName(), user.getAge()); |
| 111 | + } |
| 112 | + |
| 113 | + // PropertyEditor 应用: 局部, 使用WebDataBinder绑定 |
| 114 | + |
| 115 | + /** |
| 116 | + * TODO http://localhost:18080/databind/date?date=2017-07-09 |
| 117 | + */ |
| 118 | + @GetMapping(value = "/date") |
| 119 | + @ResponseBody |
| 120 | + public String getDate(Date date) { |
| 121 | + return date.toString(); |
| 122 | + } |
| 123 | + |
| 124 | +// @InitBinder("date") |
| 125 | +// public void initDate(WebDataBinder binder) { |
| 126 | +// binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); |
| 127 | +// } |
| 128 | + |
| 129 | + |
| 130 | + // Formatter 应用: 全局/局部 可扩展, 可实现注解配置 |
| 131 | + // spring 全局配置 |
| 132 | +// <annotation-driven conversion-service="myDateFormatter"> |
| 133 | +// </annotation-driven> |
| 134 | +// |
| 135 | +// <beans:bean id="myDateFormatter" |
| 136 | +// class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> |
| 137 | +// <beans:property name="formatters"> |
| 138 | +// <beans:set> |
| 139 | +// <beans:bean class="com.web.spring.databind.common.MyDateFormatter"></beans:bean> |
| 140 | +// </beans:set> |
| 141 | +// </beans:property> |
| 142 | +// </beans:bean> |
| 143 | + // TODO http://localhost:18080/databind/dateFormatter?date2=2017-07-09 |
| 144 | + // 注意: 传入的参数只能为date2 与方法参数保持一致 |
| 145 | + @GetMapping(value = "dateFormatter") |
| 146 | + @ResponseBody |
| 147 | + public String getFormatterDate(Date date2) { |
| 148 | + return date2.toString(); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + // Converter 应用:全局/局部 可扩展 |
| 153 | + // TODO http://localhost:18080/databind/dateConverter?date=2017-07-09 |
| 154 | + // 注意: 传入的参数只能为date2 与方法参数保持一致 |
| 155 | + @GetMapping(value = "dateConverter") |
| 156 | + @ResponseBody |
| 157 | + public String getConverterDate(Date date) { |
| 158 | + return date.toString(); |
| 159 | + } |
| 160 | +} |
0 commit comments