我有大量包含URL的XML文件.我正在编写一个groovy实用程序来查找每个URL并将其替换为更新版本.
给定example.xml:
<?xml version="1.0" encoding="UTF-8"?>
<page>
<content>
<section>
<link>
<url>/some/old/url</url>
</link>
<link>
<url>/some/old/url</url>
</link>
</section>
<section>
<link>
<url>
/a/different/old/url?with=specialChars&escaped=true
</url>
</link>
</section>
</content>
</page>
Run Code Online (Sandbox Code Playgroud)
脚本运行后,example.xml应包含:
<?xml version="1.0" encoding="UTF-8"?>
<page>
<content>
<section>
<link>
<url>/a/new/and/improved/url</url>
</link>
<link>
<url>/a/new/and/improved/url</url>
</link>
</section>
<section>
<link>
<url>
/a/different/new/and/improved/url?with=specialChars&stillEscaped=true
</url>
</link>
</section>
</content>
</page>
Run Code Online (Sandbox Code Playgroud)
这很容易使用groovy的优秀xml支持,除了我想要更改URL而不是文件的其他内容.
我的意思是:
到目前为止,在尝试了XmlParser,DOMBuilder,XmlNodePrinter,XmlUtil.serialize()等的许多组合之后,我已经逐行阅读每个文件,并应用了xml实用程序和正则表达式的丑陋混合.
读写每个文件:
files.each { File file ->
def lineEnding = file.text.contains('\r\n') ? '\r\n' : '\n'
def newLineAtEof = file.text.endsWith(lineEnding)
def lines = …Run Code Online (Sandbox Code Playgroud) 我正在使用表单数据在spring mvc中调用一个控制器.
在保存之前,我会检查id是否在某个范围内.如果id不在范围内,我需要在同一页面上显示一条消息The id selected is out of Range, please select another id within range.
我在互联网上找到了样本,如果出现问题,我可以将其重定向到失败的jsp.但在我的情况下如何做到这一点?
@RequestMapping(value = "/sendMessage")
public String sendMessage(@ModelAttribute("message") Message message,
final HttpServletRequest request) {
boolean check = userLoginService.checkForRange(message.getUserLogin());
if(!check){
return ""; //What Should I do here??????
}
}
Run Code Online (Sandbox Code Playgroud) 我在Gradle中运行Cucumber测试时遇到问题.我正在使用cucumber-jvm.
类TestNGCucumberRunner扩展了AbstractTestNGCucumberTests和testng注释@beforesuite,@aftersuite..
我通常TestNGCucumberRunner.java通过右键单击在IntelliJ中运行它并成功运行.现在我想
要么
我试图将testNGCucumberRunner.java作为javaexec执行但是失败了.
我试图执行包中的所有功能文件.我也用过apply plugin: 'com.github.samueltbrown.cucumber'.
我有这样的查询,我试图通过比较元组来过滤结果集(如IN子句中的SQL多列):
select *
from mytable
where (key, value) in (values
('key1', 'value1'),
('key2', 'value2'),
...
);
Run Code Online (Sandbox Code Playgroud)
这是有效的语法,并在我的Postgres 9.3数据库上正常工作.
我想通过Spring JDBC调用此查询,其中in值对来自a List<Map<String,String>>.
做这样的事情会很好:
List<Map<String, String>> valuesMap = ...;
String sql = "select * from mytable where (key, value) in (values :valuesMap)";
SqlParameterSource params = new MapSqlParameterSource("valuesMap", valuesMap);
jdbcTemplate.query(sql, params, rowMapper);
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到:
org.postgresql.util.PSQLException: No hstore extension installed.
at org.postgresql.jdbc2.AbstractJdbc2Statement.setMap(AbstractJdbc2Statement.java:1707) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1910) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:36) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:47) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:427) ~[spring-jdbc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:235) ~[spring-jdbc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:150) …Run Code Online (Sandbox Code Playgroud) 我有一个对象流,我想对其进行自然排序,但也强制其中一个成员始终位于第一位。
例如:
List<String> result = Stream.of("a", "s", "d", "f")
.sorted(Comparator.comparing((String s) -> !s.equals("d"))
.thenComparing(Comparator.naturalOrder()))
.collect(toList());
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
这会产生
[d, a, f, s]
Run Code Online (Sandbox Code Playgroud)
也就是说,按字母顺序排列,但以“d”开头。
我注意到我还可以使用多个.sorted(...)调用来编写此内容:
List<String> result = Stream.of("a", "s", "d", "f")
.sorted()
.sorted(Comparator.comparing(s -> !s.equals("d")))
.collect(toList());
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
这会产生相同的结果,并且在我看来更具可读性。
但是,我没有看到其他人这样做的例子。
此外,IntelliJ IDEA 将第一次.sorted()调用标记为冗余。它说“随后的‘排序’调用使排序变得毫无用处”。
这显然是不正确的,因为删除调用会将输出更改为
[d, a, s, f]
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:.sorted(...)对 Stream 的多次调用是否定义了行为,还是我只是运气好?
换句话说,这是.sorted()我可以依赖的受支持的使用,还是它只是现在才起作用,并且可能在将来的某个 Java 版本中停止工作?
我想测试一个@ModelAttribute用于其方法参数之一的控制器.
public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity)
Run Code Online (Sandbox Code Playgroud)
@ModelAttribute方法getExampleEntity是使用@RequestParam:
@ModelAttribute("exampleEntity")
public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) {
Run Code Online (Sandbox Code Playgroud)
我的控制器WebDataBinder用于调用工厂,该工厂根据参数"id"返回一个对象.
@Controller
public class ExampleController(){
@Autowired private IdEditorFactory idEditorFactory;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(ExampleEntity.class, idEditorFactory.createEditor(ExampleEntity.class));
}
@ModelAttribute("exampleEntity")
public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) {
//Irrelevant operations
return exampleEntity;
}
@RequestMapping(method = RequestMethod.POST, params = "action=save")
public String processSaveAction(
@RequestParam(value = "confirmed") String exampleString,
@ModelAttribute("exampleEntity") ExampleEntity exampleEntity, …Run Code Online (Sandbox Code Playgroud) 我知道如何让机器人模拟Y键按键,如下所示:
Robot.keyPress(KeyEvent.VK_Y);
Run Code Online (Sandbox Code Playgroud)
但是我如何让机器人按报价和句号?:
".
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供一些参考页面或示例代码吗?
未设置@EnableTransactionManager时,没有错误.
但是当设置@EnableTransactionManager时,会发生初始化错误.
这是catalina.outlog和我的java配置文件:
@Configuration
@MapperScan("net.myproject.db")
@EnableTransactionManagement
public class AppConfig {
@Value("${statdb.driverClassName}")
private String statdbDriverClassName;
@Value("${statdb.url}")
private String statdbUrl;
@Value("${statdb.id}")
private String statdbId;
@Value("${statdb.pw}")
private String statdbPw;
@Value("${statdb.validationQuery}")
private String statdbValidationQuery;
@Bean
public DataSource dataSourceStatdb() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(statdbDriverClassName);
dataSource.setUrl(statdbUrl);
dataSource.setUsername(statdbId);
dataSource.setPassword(statdbPw);
dataSource.setValidationQuery(statdbValidationQuery);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSourceStatdb());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSourceStatdb());
sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/query/*.xml"));
return sessionFactory.getObject();
}
@Bean
public …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring MVC 4.1 向一个安静的 web 服务添加速率限制。
我创建了一个@RateLimited可以应用于控制器方法的注释。Spring AOP 方面会拦截对这些方法的调用,并在请求过多时抛出异常:
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RateLimitingAspect {
@Autowired
private RateLimitService rateLimitService;
@Before("execution(* com.example..*.*(.., javax.servlet.ServletRequest+, ..)) " +
"&& @annotation(com.example.RateLimited)")
public void wait(JoinPoint jp) throws Throwable {
ServletRequest request =
Arrays
.stream(jp.getArgs())
.filter(Objects::nonNull)
.filter(arg -> ServletRequest.class.isAssignableFrom(arg.getClass()))
.map(ServletRequest.class::cast)
.findFirst()
.get();
String ip = request.getRemoteAddr();
int secondsToWait = rateLimitService.secondsUntilNextAllowedAttempt(ip);
if (secondsToWait > 0) {
throw new TooManyRequestsException(secondsToWait);
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都完美无缺,除非@RateLimited控制器方法的参数标记为@Valid,例如:
@RateLimited
@RequestMapping(method = RequestMethod.POST)
public HttpEntity<?> createAccount( …Run Code Online (Sandbox Code Playgroud) java ×4
spring-mvc ×3
spring ×2
awtrobot ×1
cucumber-jvm ×1
groovy ×1
java-8 ×1
java-stream ×1
jsp ×1
keypress ×1
mocking ×1
mockmvc ×1
postgresql ×1
regex ×1
spring-jdbc ×1
transactions ×1
unit-testing ×1
xml ×1