我知道如何List从Y- > "转换"一个简单的Java Z,即:
List<String> x;
List<Integer> y = x.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
现在我想用Map做基本相同的事情,即:
INPUT:
{
"key1" -> "41", // "41" and "42"
"key2" -> "42 // are Strings
}
OUTPUT:
{
"key1" -> 41, // 41 and 42
"key2" -> 42 // are Integers
}
Run Code Online (Sandbox Code Playgroud)
解决方案不应限于String- > Integer.就像List上面的例子一样,我想调用任何方法(或构造函数).
我有一个非常简单的问题.但到目前为止还没有找到解决方案.
所以这是我发送给服务器的JSON字符串:
{
"name" : "abc",
"groundtruth" : {
"type" : "Point",
"coordinates" : [ 2.4, 6 ]
}
}
Run Code Online (Sandbox Code Playgroud)
使用新的许可方法,我得到:
params.require(:measurement).permit(:name, :groundtruth)
Run Code Online (Sandbox Code Playgroud)
这不会引发任何错误,但创建的数据库条目包含null而不是groundtruth值.
如果我只是设置:
params.require(:measurement).permit!
Run Code Online (Sandbox Code Playgroud)
所有东西都按预期保存,但当然,这会破坏强参数提供的安全性.
我找到了解决方案,如何允许数组,但没有找到使用嵌套对象的单个示例.这必须是某种可能的,因为它应该是一个非常常见的用例.那么它是怎样工作的?
ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4
当应用程序依赖无状态身份验证(使用HMAC之类的东西)时,是否有必要使用CSRF保护?
例:
我们有一个单页应用程序(否则我们必须在每个链接上附加令牌:<a href="...?token=xyz">...</a>.
用户使用身份验证自己POST /auth.成功验证后,服务器将返回一些令牌.
令牌将通过JavaScript存储在单页面应用程序内的某个变量中.
此令牌将用于访问受限制的URL,例如/admin.
令牌将始终在HTTP标头内传输.
没有Http Session,也没有Cookies.
据我所知,应该(?!)不可能使用跨站点攻击,因为浏览器不会存储令牌,因此它无法自动将其发送到服务器(这就是使用Cookies时会发生的情况/会议).
我错过了什么吗?
authentication csrf stateless csrf-protection single-page-application
我们将在几周内开始新的Spring 4应用程序.我们想使用一些事件驱动的架构.今年我在这里和那里读到关于"Reactor"的信息,在网上搜索时,我偶然发现了"Akka".
所以现在我们有3个选择:
ApplicationEvent:http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/context/ApplicationEvent.htmlReactor:https://github.com/reactor/reactor#reactorAkka:http://akka.io/我无法找到真正的比较.
现在我们只需要:
X 注册听 Event EY 注册听 Event EZ 发送一个 Event E然后X,Y将接收并处理该事件.
我们很可能会以异步方式使用它,但肯定会有一些同步方案.我们很可能总是将一个类作为事件发送.(Reactor样本主要使用字符串和字符串模式,但它也支持对象).
据我所知,ApplicationEvent默认情况下同步Reactor工作并以异步方式工作.并且Reactor还允许使用该await()方法使其有点同步.Akka提供或多或少相同Reactor,但也支持Remoting.
关于Reactor的await()方法:它可以等待多个线程完成吗?或者甚至可能是这些线程的一部分?如果我们从上面举例:
X 注册听 Event EY 注册听 Event EZ 发送一个 Event E可以通过说:等待X 并 Y完成来使其同步.它是否有可能让它等待X,但不是为了Y?
也许还有一些替代品?例如JMS呢?
很多问题,但希望你能提供一些答案!
谢谢!
spring multithreading event-driven-design akka project-reactor
我正在使用Spring 4.0.0.RELEASE,Spring Data Commons 1.7.0.M1,Spring Hateoas 0.8.0.RELEASE
我的资源是一个简单的POJO:
public class UserResource extends ResourceSupport { ... }
Run Code Online (Sandbox Code Playgroud)
我的资源汇编程序将User对象转换为UserResource对象:
@Component
public class UserResourceAssembler extends ResourceAssemblerSupport<User, UserResource> {
public UserResourceAssembler() {
super(UserController.class, UserResource.class);
}
@Override
public UserResource toResource(User entity) {
// map User to UserResource
}
}
Run Code Online (Sandbox Code Playgroud)
在我的UserController中,我想Page<User>从我的服务中检索然后将其转换为PagedResources<UserResource>使用PagedResourcesAssembler,如下所示:https://stackoverflow.com/a/16794740/1321564
@RequestMapping(value="", method=RequestMethod.GET)
PagedResources<UserResource> get(@PageableDefault Pageable p, PagedResourcesAssembler assembler) {
Page<User> u = service.get(p)
return assembler.toResource(u);
}
Run Code Online (Sandbox Code Playgroud)
这不会调用UserResourceAssembler,只User返回内容而不是我的自定义UserResource.
返回单个资源有效:
@Autowired
UserResourceAssembler …Run Code Online (Sandbox Code Playgroud) 使用Spring 4和Hibernate 4,我能够使用Reflection从当前环境中获取Hibernate Configuration对象,使用以下代码:
@Autowired LocalContainerEntityManagerFactoryBean lcemfb;
EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) lcemfb.getNativeEntityManagerFactory();
SessionFactoryImpl sf = emf.getSessionFactory();
SessionFactoryServiceRegistryImpl serviceRegistry = (SessionFactoryServiceRegistryImpl) sf.getServiceRegistry();
Configuration cfg = null;
try {
Field field = SessionFactoryServiceRegistryImpl.class.getDeclaredField("configuration");
field.setAccessible(true);
cfg = (Configuration) field.get(serviceRegistry);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
SchemaUpdate update = new SchemaUpdate(serviceRegistry, cfg);
Run Code Online (Sandbox Code Playgroud)
对于Hibernate 5,我必须使用一些MetadataImplementor,这些对象中似乎没有.我还试图用MetadataSources同serviceRegistry.但它确实说这是错误的ServiceRegistry.
有没有其他方法让这个工作?
基本上我想将我的应用程序分成两部分.每个部分都有自己的安全性和自己@Controller的安全性.本@Services应该从两个部分进行访问.
所以我想,我应该得到2 DispatcherServlet.一听,/admin/*第二听其他一切(/).每个都有自己的,AnnotationConfigWebApplicationContext所以我可以对@Controllers 进行单独的组件扫描.
而由于春季启动提供了一个DispatcherServlet监听/开箱,我想,我可以再补充的第二个:
@Configuration
public class MyConfig {
@Bean(name="myDS")
public DispatcherServlet myDS(ApplicationContext applicationContext) {
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.setParent(applicationContext);
webContext.register(MyConfig2.class);
// webContext.refresh();
return new DispatcherServlet(webContext);
}
@Bean
public ServletRegistrationBean mySRB(@Qualifier("myDS") DispatcherServlet dispatcherServlet) {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
servletRegistrationBean.addUrlMappings("/admin/*");
servletRegistrationBean.setName("adminServlet");
return servletRegistrationBean;
}
}
Run Code Online (Sandbox Code Playgroud)
这个MyConfig2班,只有@Configuration和@ComponentScan.在同一个包中是一个@Controller.
在启动应用程序时,我可以看到第二个servlet映射已经注册,但事实@Controller并非如此.另外,我现在可以从和访问所有 . …
我最近发现了GridFS,我想将其用于带元数据的文件存储.我只是想知道是否可以使用a MongoRepository来查询GridFS?如果是的话,有人可以举个例子吗?
如果有的话,我也会使用Hibernate来解决问题.
原因是:我的元数据包含许多不同的字段,查询存储库比new Query(Criteria.where(...))为每个方案编写一些更容易.我希望也可以简单地使用Java对象并通过REST API提供它而不需要文件本身.
编辑:我正在使用
事实证明,在寻找与Solr和ElasticSearch的分页时,两者都有相同的"问题"(深度分页,尤其是分片).虽然两个搜索引擎都为此提供了解决方案/解决方法:
Solr: https :cursor //cwiki.apache.org/confluence/display/solr/Pagination+of+Results
ElasticSearch: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html#scroll-search-contextscroll
现在我读了那些页面并搜索了互联网,但在某些方面我仍然有点无能为力:
cursor/ scrolltimeouts(垃圾收集):
cursor令牌无效)的方法.这基本上只是关于可能的内存泄漏等的问题.scroll=1m.向后分页:
cursor每个请求提供令牌,因此可以访问任何以前的页面.scroll标记.所以,如果不进行新的搜索,我不能倒退?更改搜索查询:
scroll查询(http://localhost:9200/_search/scroll?scroll=1m?scroll_id=...)使用特殊URL .所以不可能改变搜索查询.cursor令牌附加到普通查询.这是否意味着,我可以使用一些cursor令牌并更改查询(过滤器,排序,页面大小等)?使用scroll/时索引更改cursor:
Solr文档说,如果文档1的排序值发生变化,使其位于光标位置之后,则文档将返回给客户端两次.这对我来说很清楚.但现在又有两个问题没有涉及:
cursor令牌(文档1在排序值更改之前)会发生什么?我会看到旧项目(包括文档1)还是会看到新生成的页面,其中包含新编计的文档?cursor令牌,我能够检索文档17吗?或者在使用当前cursor令牌序列时它是否永远消失了?ElasticSearch文档没有说明如果索引在使用时发生变化会发生什么 …
我需要对用户目标订阅做出反应.
用户订阅/user/messages,因为他想接收所有传入的消息.现在,我想查找该用户的任何消息,这些消息是在他离线时创建的,然后将它们发送给该用户.
客户代码:
stompClient.subscribe('/user/messages', function(msg){
alert(msg.body);
});
Run Code Online (Sandbox Code Playgroud)
服务器代码:
template.convertAndSendToUser(p.getName(), "/messages", "message content");
Run Code Online (Sandbox Code Playgroud)
似乎无法在服务器端捕获用户目标订阅,即:
@SubscribeMapping("/user/messages")
public void test(Principal p) {
sendMessagesThatWereReceivedWhileUserWasOffline();
}
Run Code Online (Sandbox Code Playgroud)
@SubscribeMapping("/messages")
public void test(Principal p) { ... }
Run Code Online (Sandbox Code Playgroud)
如果客户端订阅了/app/messages,这可以工作,但不会被调用 /user/messages.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/queue", "/topic");
registry.setUserDestinationPrefix("/user");
}
@Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
return true;
}
// all …Run Code Online (Sandbox Code Playgroud) spring ×6
spring-4 ×3
java ×2
pagination ×2
spring-data ×2
spring-mvc ×2
akka ×1
collectors ×1
csrf ×1
cursor ×1
gridfs ×1
hibernate ×1
java-8 ×1
java-stream ×1
lucene ×1
mapreduce ×1
mongodb ×1
servlets ×1
solr ×1
spring-boot ×1
stateless ×1
stomp ×1
websocket ×1