请NoSuchBeanDefinitionException在Spring中解释以下关于异常的内容:
本文旨在对NoSuchBeanDefinitionException使用Spring的应用程序中出现的问题进行全面的问答.
我试图使用Jackson将类值写入JSON,其中包含Optional作为字段:
public class Test {
Optional<String> field = Optional.of("hello, world!");
public Optional<String> getField() {
return field;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Test()));
}
}
Run Code Online (Sandbox Code Playgroud)
执行时,此类生成以下输出:
{"field":{"present":true}}
Run Code Online (Sandbox Code Playgroud)
我理解当前/不存在的字段被包含在内并且可以在读取JSON数据时解决它,但是我无法解决可选的实际内容永远不会写入输出的事实.:(
除了不使用ObjectMapper之外,这里有任何变通方法吗?
int totalOptCount = 500;
int totalRespCount=1500;
float percentage =(float)(totalOptCount/totalRespCount);
Run Code Online (Sandbox Code Playgroud)
为什么这总是返回值0.0?另外我想将其格式化为00.00格式并转换为字符串?
我正在尝试使用Spring的MockMVC框架测试将对象发布到数据库的方法.我构建了如下测试:
@Test
public void testInsertObject() throws Exception {
String url = BASE_URL + "/object";
ObjectBean anObject = new ObjectBean();
anObject.setObjectId("33");
anObject.setUserId("4268321");
//... more
Gson gson = new Gson();
String json = gson.toJson(anObject);
MvcResult result = this.mockMvc.perform(
post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andReturn();
}
Run Code Online (Sandbox Code Playgroud)
我正在测试的方法使用Spring的@RequestBody来接收ObjectBean,但测试总是返回400错误.
@ResponseBody
@RequestMapping( consumes="application/json",
produces="application/json",
method=RequestMethod.POST,
value="/object")
public ObjectResponse insertObject(@RequestBody ObjectBean bean){
this.photonetService.insertObject(bean);
ObjectResponse response = new ObjectResponse();
response.setObject(bean);
return response;
}
Run Code Online (Sandbox Code Playgroud)
gson在测试中创建的json:
{
"objectId":"33",
"userId":"4268321",
//... many more
}
Run Code Online (Sandbox Code Playgroud)
ObjectBean类
public class ObjectBean {
private String objectId; …Run Code Online (Sandbox Code Playgroud) class A
{
int a = 2, b = 3;
public void display()
{
int c = a + b;
System.out.println(c);
}
}
class B extends A
{
int a = 5, b = 6;
}
class Tester
{
public static void main(String arr[])
{
A x = new A();
B y = new B();
x.display();
y.display();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么输出为5,5?而不是5,11?.该y.display()方法如何工作?
我想拆分一个包含这样内容的字符串:
a$b$c
Run Code Online (Sandbox Code Playgroud)
但是当我使用时:
String data=...
data.split("$");
Run Code Online (Sandbox Code Playgroud)
它不会识别$并且不会拆分字符串,但是当我用某些字母替换$时,它会起作用.有没有人有任何想法?
我有一个ScheduledThreadPoolExecutor似乎正在吃Exception.如果提交的Runnable抛出异常,我希望我的执行程序服务通知我.
例如,我希望下面的代码至少打印出IndexArrayOutOfBoundsException的stackTrace
threadPool.scheduleAtFixedRate(
new Runnable() {
public void run() {
int[] array = new array[0];
array[42] = 5;
}
},
1000,
1500L,
TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)
作为一个附带问题.有没有办法为ScheduledThreadPoolExecutor编写一般的try catch块?
//////////原始问题的结尾//////////////
正如所建议的那样,以下装饰器运行良好
public class CatcherTask implements Runnable{
Runnable runMe;
public CatcherTask(Runnable runMe) {
this.runMe = runMe;
}
public void run() {
try {
runMe.run();
} catch (Exception ex){
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) javadoc StandardWatchEventKinds.ENTRY_MODIFY说:
目录条目已修改.为此事件注册目录时,如果观察到目录中的条目已被修改,则WatchKey将排队.此事件的事件计数为1或更高.
当您通过编辑器编辑文件的内容时,它将修改日期(或其他元数据)和内容.因此ENTRY_MODIFY,你得到两个事件,但每个事件都有count1个(至少那是我所看到的).
我正在尝试使用以下代码监视手动更新的配置文件(servers.cfg之前已注册WatchService)(即通过命令行vi):
while(true) {
watchKey = watchService.take(); // blocks
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
WatchEvent.Kind<Path> kind = watchEvent.kind();
System.out.println(watchEvent.context() + ", count: "+ watchEvent.count() + ", event: "+ watchEvent.kind());
// prints (loop on the while twice)
// servers.cfg, count: 1, event: ENTRY_MODIFY
// servers.cfg, count: 1, event: ENTRY_MODIFY
switch(kind.name()) {
case "ENTRY_MODIFY":
handleModify(watchEvent.context()); // reload configuration class
break;
case "ENTRY_DELETE": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Springs java-config为我的应用程序上下文运行Junit功能测试.我不确定它是否是Spring或Junit的问题...只是不确定..应用程序在我的本地服务器上运行正常(击中数据库),但是当我尝试运行我的测试时它会爆炸.我刚刚从xml移动到java配置,所以我总是可以导入我的xml上下文文件(我知道在我的测试中可以工作),但是我想继续使用我的java配置.
我的测试班
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
classes = { AppConfig.class, ServletConfig.class })
@EnvironmentConfiguration(db2Enabled = true)
public class EventDAOTest {
Run Code Online (Sandbox Code Playgroud)
StackTrace
Feb 4, 2014 11:51:45 AM com.aoins.config.CachingPropertyFileReader <clinit>
SEVERE: ERROR - Configuration Framework not detected!
Feb 4, 2014 11:51:45 AM com.aoins.config.CachingPropertyFileReader readFile
SEVERE: Unable to build property reference for: (common).
2014-02-04 11:51:45,510 | DEBUG | : | LoggerConfigurer | Msg: Log4j configured with: 'C:/javalog/log4jPCProperties.xml'
2014-02-04 11:51:48,248 | INFO | : | EnvironmentConfigurer | Msg: DB2 Connection was established
2014-02-04 11:51:48,279 …Run Code Online (Sandbox Code Playgroud) 在典型的Spring MVC Web应用程序中,您可以像这样声明DispatcherServletinweb.xml
<!-- MVC Servlet -->
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
与听众,过滤器等一起
使用servlet-api 3.0,您可以使用注释声明servlet,@WebServlet而不是将它们添加到您的web.xml.春季3.2已经@Configuration和@EnableXYZ它的上下文配置.是否有类似的东西DispatcherServlet,即.有没有办法配置你的完整的Spring应用程序没有任何xml?
java ×8
spring ×2
spring-mvc ×2
annotations ×1
file ×1
inheritance ×1
jackson ×1
java-8 ×1
junit4 ×1
nio ×1
threadpool ×1
watchservice ×1