我有一个Maven配置文件,它触发xsd和wsdl类的自动生成,如下所示:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${cxf-xjc-plugin}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/src/main/java</sourceRoot>
<xsdOptions>
//xsds, wsdls etc
</xsdOptions>
</configuration>
<goals>
<goal>xsdtojava</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
生成的类转到:target/generated/src/main/java。
问题:运行“ mvn clean package”将始终删除那些类。我该如何预防?是否可以clean删除target目录中的全部内容generated/?
我有一个Tasklet并想计算已处理的项目。然后,公共StepExecutionListener应该能够读取这些已处理的项目计数afterStep():
@Bean
public Step myStep() {
return stepBuilderFactory.get("Step2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
int items = dao.deleteItems(); //how to pass these items to a StepExecutionListener?
return RepeatStatus.FINISHED;
}
})
.build();
@Component
public class MyListener extends StepExecutionListenerSupport {
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
long items = stepExecution.getWriteCount();
return super.afterStep(stepExecution);
}
}
Run Code Online (Sandbox Code Playgroud)
如何将处理后的项目放入stepExecutiontasklet 中?
我想整理一个简单的清单.示例Integers:
List<Integer> numbers = Arrays.asList(4, 2, 7, 1, 4);
numbers.stream().sorted((n1, n2) -> {
System.out.println("comparing: " + n1 + " with " + n2);
return Integer.compare(n1, n2);
});
Run Code Online (Sandbox Code Playgroud)
结果:没有打印出来!没有应用排序.因此,甚至不执行排序方法.为什么?
旁注:我的目的是直接修改列表(在一个对象中),因此我试图阻止创建一个新列表.
我有一个简单的servlet,如下所示:
@RestController
public class TestServlet {
@RequestMapping(value = "/test1")
public String test1() {
return "test1";
}
@RequestMapping(value = "/test2")
public String test2(@RequestBody TestClass req) {
return "test2";
}
public static class TestClass {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,只有不接收参数的servlet才起作用:
作品: http://localhost:8080/test1
不起作用: http://localhost:8080/test2?value=1234
org.springframework.http.converter.HttpMessageNotReadableException:缺少必需的请求正文:public java.lang.String
为什么@RequestBody注释不起作用?我错过了重要的一块吗?
我想提取两个分隔符之间找到的所有数字-.
例:
test-555-2468-123
Run Code Online (Sandbox Code Playgroud)
提取的理想结果:
555
2468
Run Code Online (Sandbox Code Playgroud)
我尝试使用正则表达式如下:[\d+]+.这至少给了我一个块中的所有数字.但是,如何添加数字必须在-字符前后固定的限制?
我有一个简单的,@RestController并希望响应两者JSON或XML取决于http标头content-type.
问题:我总是只得到XML回应,而不是JSON.当然我Content-Type: application/json用作http标头.
以下配置中可能缺少什么?
@RestController
public void MyServlet {
@RequestMapping(value = "test", method = RequestMethod.GET,
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public MyResponse test() {
return new MyResponse();
}
}
@XmlRootElement
public class MyResponse {
private String test = "somevalue";
//getter, setter
}
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<!-- as advised in: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
有趣的是:如果我切换生产声明:
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})那么我总是JSON走出去,永远不会XML!
所以问题是:为什么第一个MediaType总是有优先权,http头从不考虑?
我使用Future和创建一些异步任务Callable<>.问题:如果Exception在异步执行期间发生了,我需要访问用于构建可调用对象的参数.但是怎么样?
例:
List<Callable<Response> tasks = new ArrayList<>();
taks.add(() -> sendRequest(req));
futures = executor.invokeAll(tasks);
for (future : futures) {
try {
Response rsp = future.get();
} catch (ExecutionException e) {
//problem: I need to access req.getType() value of the req Object here. But How?
}
}
Run Code Online (Sandbox Code Playgroud)
喜欢:我想从中收集所有请求值req.getType(),因此我知道哪些异步请求失败.并将错误消息返回给用户.
我想返回204 no contenthttp状态代码。尽管我想添加一个自定义错误消息,其中提供了详细说明为何不存在任何内容的细节。
问题:我正在使用spring-mvc,并且在返回HttpStatus.NO_CONTENT类型时,响应主体始终被删除,并且对于客户端而言是空的!
@RestControllerAdvice
public class ExeptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.NO_CONTENT)
public Object handler(HttpServletRequest req, Exception e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将类型更改为例如,HttpStatus.NOT_FOUND则错误消息将显示为响应正文。
如何使用204达到同样的效果?
我正在通过json网络服务输出一些数据库结果。简单如下:
@GetMapping(produces = "application/json")
public List<Map<String, Object>> get(Param params) {
return jdbcTemplate.queryForList(sql, params)
}
Run Code Online (Sandbox Code Playgroud)
问题:java.sql.Timestamp转换为 format 2018-04-26T07:52:02.000+0000,而纯数据库输出将为2018-04-26 07:52:02.0.
问题:是否有任何配置属性告诉spring只传递从数据库接收到的本机时间戳,而不是用jackson逻辑转换它?
我想全局java.sql.Timestamp更改格式。
重要提示:请不要建议任何注释!我没有任何 bean/pojo,我只是将纯数据库结果作为Map.
我有一个比较对象的嵌套字段的比较器。这里的pricea product。
问题:如果该字段为空,我将收到 NullPointerException。
问题:如何告诉比较器忽略比较字段为空的对象,而无需(!)事先过滤列表?忽略我的意思是将它们放在列表的末尾。
public class SorterTest {
private static final Comparator<Product> PRODUCT_COMPARATOR =
Comparator.comparing(p -> p.details.price);
static class Product {
String name;
Details details;
static class Details {
BigDecimal price;
}
}
@Test
public void test() {
List<Product> products = Arrays.asList(
createProduct("A", new BigDecimal(30.00)),
createProduct("B", new BigDecimal(55.00)),
createProduct("C", new BigDecimal(20.00)),
createProduct("D", null),
createProduct("E", null),
createProduct("F", null)
);
Collections.sort(products, PRODUCT_COMPARATOR);
assertEquals("C", products.get(0).name);
assertEquals("A", products.get(1).name);
assertEquals("B", products.get(2).name);
assertEquals("D", products.get(3).name);
assertEquals("E", products.get(4).name);
assertEquals("F", products.get(5).name);
}
private Product createProduct(String …Run Code Online (Sandbox Code Playgroud) java ×10
spring ×5
json ×2
spring-mvc ×2
comparator ×1
cxf ×1
future ×1
httpresponse ×1
jackson ×1
java-8 ×1
java-stream ×1
maven ×1
regex ×1
rest ×1
servlets ×1
spring-batch ×1
spring-web ×1
xjc ×1
xml ×1