发表于: 2017-07-27 23:03:21

1 961


【说明】以后晚上睡觉之前一定要把日报给写了

一:今日完成

1,有一个问题,如果前端传json 传的是多个不同对象,不可以使用requestBody 多个注解

这个需要清楚的了解request

请求参数永远都是一个,因为一个 request 中只包含一个 request body.

所以,是不可能的

那么怎么解决呢?

三个解决方法

1,创建一个新的 entity,将其它两个 entity 都进去

2,用Map<String, Object> 接收request body,然后自己反序列化到 各个model里面

3, 还可以实现自己的HandlerMethodArgumentResolver

这个有点难度呢

Most of the Spring controllers in the real world accept a lot of different types of parameters - Path variables, URL parameters, request headers, request body and sometimes even the entire HTTP Request object. 

张帆看看这个

Spring is really good at parsing these parameters in to Java types as long as there is an ObjectMapper (like Jackson) configured to take care of the de-serialization.

spring全能

Spring provides a convenient way to provide such custom arguments. 

参数是怎么解析的

All argument resolvers implement the HandlerMethodArgumentResolver interface whose contract is:

是这样子的

直接例子吧

在下面链接里面

这是请求数据

怎么解析呢

在下面链接里面

Using in a controller method

看看控制方法

也是解析了之后直接使用

 

@RestController 

@RequestMapping("/profiles") 

public class ProfileController { 

 @Autowired    ProfileService profileService;    @RequestMapping(path = "/",    method = RequestMethod.POST)    public Map<String, String> create(@RequestBody Profile profile, MetaData metaData) {        profileService.create(profile, metaData); 

 System.out.printf(metaData.toString()); 

 return Collections.singletonMap("message", "success"); 

 } }

重写

在下面链接里面

Wiring up the custom resolver

加到解析器里面

it needs to be added to the chain of resolvers that Spring uses.

自己写

@Configuration public class Config extends WebMvcConfigurerAdapter { 

 @Override    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {        argumentResolvers.add(new MetaDataResolver());    } }

测试
test the behavior of a controller in the presence of a
custom argument resolver using MockMvc,
具体测试省了
可以的话大家去试试
网址是
https://sdqali.in/blog/2016/01/29/using-custom-arguments-in-spring-mvc-controllers/
2016写的,还是挺新的!!

2,获取cookie 的方式之一

@ResponseBody

@RequestMapping("cookie")

public String cookie(@CookieValue("x")String x){

       return x;

}

 
 
这个注解没有使用过呢
3,还是绑定对象
一个 User,一个 Phone,一个 User 拥有多个 Phone,为了演示,User 中有一个 List 和 Array 的 Phone 的集合
 html绑定
 
一对多的时候,使用多方的在一方的对象中的属性名,加上数组下标,
即 phones[0].brand,
phones[1].brand
即可绑定到 User 的 phones 属性上,
这种方法的局限性就是要求下标是正确的,
否则会无法绑定,不是很方便,但是也有其适用场景。
再来一个 绑定 post 方法的

加上json 解析

<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

 再注册到messageConverters

String user2(@RequestBody List<User> user)

这个需要js 传值

类似的还有传 字符串数组

又看到一个奇葩的办法

@PathVariable("id")Long[] id

然后在路径里面输入5,4,3,2,1

没想到这个也可以

再来一个

如果表单对应多个实体类

这些类里面有相同的属性,,麻烦了

A   x

B   x

C   A  B

然后在springMVC 里面 接收 C

c.getA().getX()

c.getB().getX()

还有获取session 的这个有点看不懂

放上地址吧

http://blog.csdn.net/z69183787/article/details/41653843/


3.说一个 spring mvc 下面的excel 导出

新建类继承 AbstractExcelView

处理中文名字

 Controller 里面返回 ModelAndView 即可

这个可以试一下

http://blog.csdn.net/z69183787/article/details/41647925

二:明日计划

明天因为时间充足(or 有限),打算完成

①简历 

------------------

项目经验

------------------

③技术整理

------------------

三:疑难问题

四:思考总结

Ⅰ 对自己今天的进度满意吗

------------------一般般

Ⅱ 那些困扰自己的问题所在

------------------自己到底掌握了哪些东西

Ⅲ 暗示自己为明天加油打劲

------------------给点阳光要灿烂




返回列表 返回列表
评论

    分享到