我正在使用spring boot并且我创建了以下自定义错误控制器来处理错误:
@Controller
public class AppErrorController implements ErrorController {
private static final String PATH = "/error";
@RequestMapping(value = "/pageNotFound", method = { RequestMethod.GET, RequestMethod.POST })
public String pageNotFound() {
return "pageNotFound";
}
@RequestMapping(value = "/accessDenied", method = { RequestMethod.GET, RequestMethod.POST })
public String accessDenied() {
return "accessDenied";
}
@RequestMapping(value = PATH)
public String error() {
return "error";
}
@Override
public String getErrorPath() {
return PATH;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在应用程序中收到错误时,它应该重定向到自定义错误页面.相反,我在日志中收到以下错误,甚至错误页面都没有显示:
2016-03-09 09:43:21.224 DEBUG 3126 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method …
Run Code Online (Sandbox Code Playgroud) 错误是:
/usr/lib/jvm/java-8-oracle/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:40745,suspend=y,server=n -Dspring.output.ansi.enabled=always -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jr main] o.s.b.c.c.ConfigFileApplicationListener : Skipped (empty) config file 'file:./application.properties' for profile default
2016-03-25 23:43:41.426 DEBUG 18169 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Skipped (empty) config file 'classpath:/application.properties' for profile default
2016-03-25 23:43:41.460 INFO 18169 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3738449f: startup date [Fri Mar 25 23:43:41 EET 2016]; root of context hierarchy
2016-03-25 23:43:44.256 DEBUG 18169 --- [ main] o.s.b.a.AutoConfigurationPackages : @EnableAutoConfiguration was declared on a class in the package 'org.infoowl'. Automatic @Repository and …
Run Code Online (Sandbox Code Playgroud) 如何配置的想法自动替换=>
用?
和->
用?
?
对我来说,下面是异步和非阻塞I/O最可能的定义:
Asynchronous I/O:
在异步I/O应用程序中立即返回,OS将在字节可用于处理时让他们知道.
NON-blocking I/O:
这里应用程序立即返回可用的数据和应用程序应该具有轮询机制以找出何时准备好更多数据.
知道这些定义,如果我们分析Java通道即后SocketChannel
,ServerSocketChannel
,DatagramSocketChannel
那么我们可以发现,这些信道可以被用作阻挡或通过该方法非阻塞模式configureBlocking(boolean block)
.并假设我们将它们用作非阻塞模式.所以这里有问题:
如果我将使用Selector
ie寄存器通道来selector
确定它是异步I/O还是非阻塞I/O?
我觉得这是java中的异步I/O,当且仅当底层操作系统通知java应用程序有关通道的准备选择时.否则它是非阻塞I/O,并且selector
正如我在定义中提到的那样,它只是帮助我们轮询上述通道的机制.哪个是对的?提前致谢.
编辑:
我已经回答了问题的一部分,即I/O的类型以及java如何促进这些功能.
但仍有一个问题仍然是java是否提供所有这些功能是在java层模拟还是使用底层操作系统来促进?假设底层操作系统具有对这些功能的所有支持.
请参考答案.
我想用Spring Boot编写一个小而简单的REST服务.这是REST服务代码:
@Async
@RequestMapping(value = "/getuser", method = POST, consumes = "application/json", produces = "application/json")
public @ResponseBody Record getRecord(@RequestBody Integer userId) {
Record result = null;
// Omitted logic
return result;
}
Run Code Online (Sandbox Code Playgroud)
我发送的JSON对象如下:
{
"userId": 3
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
WARN 964 --- [XNIO-2 task-7] .wsmsDefaultHandlerExceptionResolver:无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法从START_OBJECT反序列化java.lang.Integer的实例令牌在[来源:java.io.PushbackInputStream@12e7333c; line:1,column:1]; 嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法在[来源:java.io.PushbackInputStream@12e7333c;中的START_OBJECT标记中反序列化java.lang.Integer的实例.line:1,column:1]
我正在使用Jetty 9.1.0.RC2和Spring 4.有一个AbstractAnnotationConfigDispatcherServletInitializer
并尝试启动初始化:
Server server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setConfigurations(new Configuration[] { new AnnotationConfiguration() });
webAppContext.setParentLoaderPriority(true);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/target/classes/.*");
server.setHandler(webAppContext);
server.start();
server.join();
Run Code Online (Sandbox Code Playgroud)
但未能发现:
No Spring WebApplicationInitializer types detected on classpath
Run Code Online (Sandbox Code Playgroud) 我对spring数据mongodb存储库有一个奇怪的问题..我想从我的findAll
请求中排除一个字段.我怎样才能做到这一点?
这非常有效:
@Query(fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findByObjectIdAndServiceIgnoreCase( String objectId, String service, Pageable pageable );
Run Code Online (Sandbox Code Playgroud)
但没有机会findAll
:
@Query(fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findAll( Pageable pageable );
Run Code Online (Sandbox Code Playgroud)
抛出:
引起:org.springframework.data.mapping.PropertyReferenceException:找不到类型为ObjectHistory的属性findAll!org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75)位于org.springframework.data.mapping.PropertyPath.create的org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) PropertyPath.java:307)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)atg.springframework.data .repository.query.parser.Part.(Part.java:76)
我在Java + Spring中不是很好,但我想Cache-Control
在我的头文件中添加标题ResponseEntity
.
@RequestMapping(value = "/data/{id}", method = GET")
public ResponseEntity<String> getData(@PathVariable("id") String id) {
try {
...
HttpHeaders headers = new HttpHeaders();
headers.setCacheControl("max-age=600");
return new ResponseEntity<String>(body, headers, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了两行代码HttpHeaders
,现在Cache-Control
我的响应中有两个标题:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
Cache-Control: max-age=600
Content-Type: application/json;charset=UTF-8
Content-Length: 18223
Date: Wed, 29 Jun 2016 21:56:57 GMT
Run Code Online (Sandbox Code Playgroud)
我做错了什么?有人能帮助我吗?
双方HandlerInterceptor
并HandlerInterceptorAdaptor
具有preHandle
和postHandle
方法.但我无法理解它们在实施方面有何区别.
一些关于Spring国际化的文章讲述了如何交换传递语言环境等的消息,但我只发现包含一些消息的用例.
如何根据上下文组织和使用国际化文件?(验证,查看消息,默认消息,业务消息)
我知道Spring使用模式(定义的消息文件的名称)+ locale.例如:message_zh_CN.如何知道这种行为的每个上下文文件?
我认为它应该是:
resources
`-- messages
|-- validation
| |-- message_locale.properties
| `-- message_locale2.properties
|-- business
| |-- message_locale.properties
| `-- message_locale2.properties
`-- view
|-- message_locale.properties
`-- message_locale2.properties
Run Code Online (Sandbox Code Playgroud)
要么:
resources
`-- messages
|-- validation
| |-- validation_locale.properties
| `-- validation_locale2.properties
|-- business
| |-- business_locale.properties
| `-- business_locale2.properties
`-- view
|-- view_locale.properties
`-- view_locale2.properties
Run Code Online (Sandbox Code Playgroud) java ×9
spring ×8
spring-boot ×4
spring-mvc ×3
asynchronous ×1
io ×1
jackson ×1
mongodb ×1
nio ×1
nonblocking ×1
repository ×1
scala ×1
unicode ×1