使用Spring,我有一个SimpleMappingExceptionResolver,可以在resolveException方法中捕获应用程序中的任何意外异常.在该方法中,我返回一个ModelAndView,它将错误消息文本提供给HTTP客户端.这是代码:
public class UnExpectedExceptionResolver extends SimpleMappingExceptionResolver {
private Log logger = LogFactory.getLog(this.getClass().getName());
private ResourceBundleMessageSource messageSource;
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
// let the end user know that an error occurred
Locale locale = RequestContextUtils.getLocale(request);
String messageText = messageSource.getMessage("systemError", null, locale);
Message message = new Message(messageText, MessageType.ERROR);
ModelAndView mav = new ModelAndView();
mav.setView(new MappingJacksonJsonView());
mav.addObject("message", message);
return mav;
}
Run Code Online (Sandbox Code Playgroud)
因此,响应将返回HTTP状态代码200,响应文本为消息(JSON).不幸的是,客户认为这是一个有效的响应,因为200代码并试图处理它.我尝试将HTTP状态代码设置为500,如下所示:
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server Error");
Run Code Online (Sandbox Code Playgroud)
就在之前
return mav;
Run Code Online (Sandbox Code Playgroud)
声明.不幸的是,这会返回一个HTML页面,指示内部错误而不是我的JSON消息.如何返回JSON消息并仍向客户端指示服务器错误(或某种类型的错误)?具体来说,我希望调用AJAX调用中的客户端错误函数,并将消息数据发送回客户端.仅供参考 - 我在客户端使用jQuery.
我有一个Spring Boot Batch应用程序,我正在编写集成测试.当我执行测试时,整个批处理应用程序运行.如何只执行测试中的应用程序代码?
这是我的测试代码.执行时,将运行整个批处理作业步骤(读取器,处理器和编写器).然后,测试运行.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BatchApplication.class))
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
public class StepScopeTestExecutionListenerIntegrationTests {
@Autowired
private FlatFileItemReader<String> reader;
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
public StepExecution getStepExection() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
return execution;
}
@Test
public void testGoodData() throws Exception {
//some test code on one met
File testFile = testFolder.newFile();
PrintWriter writer = new PrintWriter(testFile, "UTF-8");
writer.println("test");
writer.close();
reader.setResource(new FileSystemResource(testFile));
reader.open(getStepExection().getExecutionContext());
String test = reader.read();
reader.close();
assertThat("test", equalTo(test));
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个需要每天运行的 Spring Boot Batch 应用程序。它读取每日文件,对其数据进行一些处理,并将处理后的数据写入数据库。在此过程中,应用程序保存一些状态,例如要读取的文件(存储在 和 中FlatFileItemReader)JobParameters、运行的当前日期和时间、用于在读取项目之间进行比较的一些文件数据等。
调度的一种选择是使用 Spring,@Scheduled例如:
@Scheduled(cron = "${schedule}")
public void runJob() throws Exception {
jobRunner.runJob(); //runs the batch job by calling jobLauncher.run(job, jobParameters);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是在运行之间保持状态。因此,我必须更新要读取的文件、运行的当前日期和时间、清除缓存的文件数据等。
另一种选择是通过 unix cron 作业运行应用程序。这显然可以满足在运行之间清除状态的需要,但我更喜欢将作业调度与应用程序而不是操作系统联系起来(并且更喜欢与操作系统无关)。@Scheduled可以在运行之间重置应用程序状态吗?
我想get通过 SFTP 使用 SFTP 出站网关访问文件,但我只找到使用 XML 配置的示例。如何使用 Java 配置来完成此操作?
更新(感谢 Artem Bilan 帮助)
我的配置类:
@Configuration
public class MyConfiguration {
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory();
sftpSessionFactory.setHost("myhost");
sftpSessionFactory.setPort(22);
sftpSessionFactory.setUser("uname");
sftpSessionFactory.setPassword("pass");
sftpSessionFactory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(sftpSessionFactory);
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "get", "#getPayload() == '/home/samadmin/test.endf'");
sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
return sftpOutboundGateway;
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用类:
@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {
public static void main(String[] args) {
SpringApplication.run(TestIntegrationApplication.class, args);
} …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring 集成应用程序,一旦文件存在于本地目录中,它就会对其进行一些处理。处理文件后,会将文件移动到已处理的目录。
一段时间后,同一本地目录中出现一个新文件,其文件名相同,但内容和时间戳不同。应用程序应该再次处理该文件,然后将其移动到已处理的目录...但永远不会生成消息。这是配置:
@Bean
@InboundChannelAdapter(value = "dailyFileInputChannel", poller = @Poller(maxMessagesPerPoll = "1", fixedDelay = "${load.manualPollingInterval}"))
public MessageSource<File> messageSource(ApplicationProperties applicationProperties) {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(applicationProperties.getLoadDirectory());
CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<File>();
compositeFileListFilter.addFilter(new LastModifiedFileListFilter());
FileSystemPersistentAcceptOnceFileListFilter persistent = new FileSystemPersistentAcceptOnceFileListFilter(store(), "dailyfilesystem");
persistent.setFlushOnUpdate(true);
compositeFileListFilter.addFilter(persistent);
compositeFileListFilter.addFilter(new SimplePatternFileListFilter(applicationProperties.getFileNamePattern()));
source.setFilter(compositeFileListFilter);
return source;
}
@Bean
public PropertiesPersistingMetadataStore store() {
PropertiesPersistingMetadataStore store = new PropertiesPersistingMetadataStore();
store.setBaseDirectory(applicationProperties.getProcessedStoreDirectory());
store.setFileName(applicationProperties.getProcessedStoreFile());
return store;
}
@Bean
@ServiceActivator(inputChannel = "dailyFileInputChannel")
public MessageHandler handler() {
// return a handler that processes and moves the …Run Code Online (Sandbox Code Playgroud) 我有一个带滚动条的Kendo窗口.如果用户向下滚动到窗口内容的底部,关闭窗口,然后重新打开,则窗口打开时处于相同的滚动位置(即窗口内容的底部).但是,我希望窗口在重新打开时始终显示在内容的顶部.如何才能做到这一点?
这是证明问题的jsfiddle:
这里有一些来自小提琴的代码,因为我必须将这个包含在帖子中...
var win;
function openWindow() {
if (!win) {
win = $('#win').kendoWindow({
modal: true,
width: '100px',
height: '100px'
});
}
$('#win').css('display', '');
win.data('kendoWindow').center().open();
}
$(document).ready(function() {
$('#button').click(openWindow);
});
Run Code Online (Sandbox Code Playgroud) 如何让 Spring Batch 在一段时间后重试失败的步骤?
如果该步骤失败超过 x 次,我希望不再重试该步骤并使整个作业失败。
我正在使用 Java Config,并且不喜欢使用 XML 来配置重试。
使用行数据模板或使用 HTML 作为字段值的列标题模板,我可以向 Kendo UI 网格添加一个复选框。例如:
<div id="grid"></div>
<script>
$(document).ready(function() {
$("#grid").kendoGrid({
columns: [{
field:'<input id="masterCheck" type="checkbox" /><label for="masterCheck"></label>',
width: 33,
height: 550,
}]
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
但是,该复选框的样式并不符合 Kendo UI 主题。将 class="k-checkbox" 添加到输入复选框元素应根据主题设置其样式。但是,当我将该类应用于复选框时,该复选框不再可见。如何让 k 复选框在网格中可见?
该问题的示例位于http://dojo.telerik.com/AjuFo
Solr报告它缺少必填字段(documentId),但字段和值正传递给Solr.从架构:
<fields>
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="documentId" type="string" indexed="true" stored="true" required="true" />
</fields>
Run Code Online (Sandbox Code Playgroud)
根据Solr日志,documentId正在传入:
org.apache.solr.core.SolrCore execute
INFO: [] webapp=/solr path=/update/extract params={waitSearcher=true&commit=true
&literal.id=C:\documents\102\Comps+Database+BRD.docx&literal.documentId=102&w
t=javabin&waitFlush=true&version=2} status=400 QTime=489
Run Code Online (Sandbox Code Playgroud)
为什么Solr会报告:
org.apache.solr.common.SolrException log
SEVERE: org.apache.solr.common.SolrException: [doc=C:\documents\102\test.docx]
missing required field: documentId
Run Code Online (Sandbox Code Playgroud)
编辑 提取请求中更正的拼写错误,但仍然是相同的错误.
使用JPA,我正在调用看起来像这样的MS SQL Server 2008 R2存储过程
procedure [dbo].[testProc]
@param1 varchar(max),
@param2 datetime
as
begin
EXEC sessionProc
DECLARE @reportData varbinary(max)
EXEC aThirdPartyProc
@reportData out,
@parameter1 = @param1,
@date = @param2
SELECT col1, col2
FROM fFunction(@reportData)
end
Run Code Online (Sandbox Code Playgroud)
尝试从select语句获取结果时
StoredProcedureQuery q = em.createNamedStoredProcedureQuery("reportData");
q.setParameter("param1", "val1");
q.setParameter("param2", new Date());
return (List<ReportData>) q.getResultList();
Run Code Online (Sandbox Code Playgroud)
我懂了
java.lang.IllegalStateException: Current CallableStatement ou was not a ResultSet, but getResultList was called
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getResultList(StoredProcedureQueryImpl.java:319)
Run Code Online (Sandbox Code Playgroud)
如何获得选择语句的结果?
注意:如果我将testProc简化为一个简单选择(删除两个EXEC语句),则JPA代码有效。
另外,这是ReportData实体类:
@Entity
@NamedStoredProcedureQuery(name = "reportData", procedureName = "testProc", resultClasses = ReportData.class, parameters = {
@StoredProcedureParameter(mode = …Run Code Online (Sandbox Code Playgroud) spring ×4
spring-batch ×3
java ×2
kendo-ui ×2
spring-boot ×2
ajax ×1
css ×1
exception ×1
hibernate ×1
javascript ×1
jpa ×1
jquery ×1
kendo-grid ×1
schedule ×1
scroll ×1
solr ×1
spring-retry ×1
spring-test ×1
sql-server ×1
telerik ×1
telerik-grid ×1
window ×1