我有一个 Spring MVC 控制器和一个异常处理程序。发生异常时,我希望异常处理程序记录请求中发送的所有 GET/POST 数据。如何做到这一点?
控制器:
@Controller
@RequestMapping("/foo")
public class FooController {
private final FooService fooService;
@PostMapping("/bar")
@ResponseBody
public BarObject doSomething(@RequestBody final FooContext context)
{
return fooService.doSomething(context);
}
}
Run Code Online (Sandbox Code Playgroud)
异常处理程序:
@ControllerAdvice
public class ExceptionController {
private final Logger log = LoggerFactory.getLogger(ExceptionController.class);
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
@ResponseBody
public ErrorMessage handleException(final HttpServletRequest request, final Exception exception) {
//Retrieve request data
//request.getQueryString()
// How to get POST data if we cannot access @RequestBody?)
log.error(request.getRequestURI(), exception);
return new ErrorMessage(request.getRequestURI(), exception.getLocalizedMessage());
}
Run Code Online (Sandbox Code Playgroud) 我有两个非常大ArrayList,每个包含数百万的数据.我想过滤掉List1不存在的数据List2和/或反之亦然.
我已经尝试过Apache CollectionUtils,Java 8流API但没有任何成功.
Java 8并行流消耗所有CPU,而CollectionUtils继续比较没有任何输出的数据集.
POJO样本
public DataVO {
private String id;
private String value;
...
// getters / setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
...
...
final DataVO other = (DataVO) obj;
if (id == null) {
if (other.id != …Run Code Online (Sandbox Code Playgroud) 有很多问题让人们意识到用一个表达式来计算一个null值来创建一个方法引用会产生一个NullPointerException.举个例子:
String s = null;
Supplier<char[]> fun = s::toCharArray;
Run Code Online (Sandbox Code Playgroud)
这是由于java规范中的以下段落:
首先,如果方法引用表达式以ExpressionName或Primary开头,则计算此子表达式.如果子表达式的计算结果为null,则会引发NullPointerException,并且方法引用表达式会突然完成.如果子表达式突然完成,则方法引用表达式会出于同样的原因突然完成.
现在我的问题是,有没有人碰巧知道背后的原因(基于许多困惑的问题)反直觉规范是什么?
我唯一想到的是,在以下情况下,很难准确地报告NullPointerException在评估期间发生的错误Supplier:
public static char[] callback(Supplier<char[]> supplier) {
return supplier.get();
}
public static void main(String[] args) {
String s = null;
callback(s::toCharArray);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试如下代码
int[][] mat = new int[][]{new int[]{2,4,6}, new int[]{8,9,12}};
int oddInAll = Arrays.stream(mat).filter(i -> i%2 != 0).findFirst().getAsInt();
Run Code Online (Sandbox Code Playgroud)
但这给了我错误
| 错误:| 二元运算符'%'|的坏操作数类型 第一种类型:int [] | 第二种类型:int | int oddInAll = Arrays.stream(mat).filter(i - > i%2!= 0).findFirst().getAsInt(); |
^ ^ ---| 错误:| 找不到符号| symbol:方法getAsInt()|
int oddInAll = Arrays.stream(mat).filter(i - > i%2!= 0).findFirst().getAsInt(); |
^ ------------------------------------------------- ------------ ^
这次我错过了什么?
import java.util.*;
import static java.lang.String.format;
public class Dumpground {
private static final String[] fruits = new String[]{"apples", "bananas", "grapes", "oranges", "watermelons", "kiwis"};
static Map<String, Long> expirationMap;
public static void main(String[] args) {
long expiration = 1L;
expirationMap = new HashMap<>();
for (String fruit : values()){
expirationMap.put(fruit, expiration);
expiration++;
}
for (Map.Entry<String, Long> item : expirationMap.entrySet()) {
String key = item.getKey();
Long value = item.getValue();
System.out.println(format("key: %s, value: %s", key, value));
}
}
public static String[] values() {return fruits;}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT …
在 Angular4 中,您可以使用loadChildren路由配置中的属性延迟加载模块pathToMyModule#MyModule。我想知道是否可以指定任何属性来始终加载我的模块(因此基本上禁用延迟加载)。
我有一个数组列表,其中包含构造函数所需的所有参数.引用数组中的每个项目然后分别提供参数似乎很多工作.我想知道我是否可以通过在构造函数的括号内迭代它来传递数组列表中的每个项目.
我问我是否可以这样做,或类似的东西传递参数.
constructor object =new constructor(for(String item: parts));
Run Code Online (Sandbox Code Playgroud)
parts是这里的数组列表.列表中的所有项目都是字符串.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Delete
{
public static void main(String[] args)
{
List<List<String>> list = new ArrayList<>();
list.add(List.of("A","B","C","R"));
list.add(List.of("E","F","G","F"));
list.add(List.of("A","B","C","D"));
System.out.println(list.stream().distinct().count());
Map<String, Long> countMapOfColumn = list.stream()
.filter(innerList -> innerList.size() >= 3)
.map(innerList -> innerList.get(3 - 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(countMapOfColumn.keySet());
}
}
Run Code Online (Sandbox Code Playgroud)
我想找出第3列中的唯一元素,第3列中"C","G"有2唯一元素.
这可以使用loop但我不能使用循环.使用java stream可能有一个解决方案.
另外,我如何获得一次"A","B"列中列的行数以及列中的行数?1,2N
如何在最新的phpMyAdmin中创建众所周知的导出为SQL?SQL别无选择!
我是java的新手(2周),我正在尝试将输入的字符串转换为ascii代码并尝试打印总和.
我尝试过使用IntStream.of(AsciiArray).sum但是由于它是一个字符串而不是一个int它不起作用(可理解)
import java.util.Arrays;
import java.util.Scanner;
public class Strings{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a 5 letter word (all lower case): ");
String word = input.nextLine();
int length = word.length();
byte[] bytes = word.getBytes();
String AsciiArray = Arrays.toString(bytes);
System.out.println("the ascii sum is" + AsciiArray);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的一个例子:"输入一个5个字母的单词(全部小写):"
你好
ascii总和是532
我得到了什么:
"输入一个5个字母的单词(全部小写):"
你好
ascii总和是[104,101,108,108,111]
java ×8
java-8 ×4
java-stream ×3
arrays ×2
lambda ×2
angular ×1
arraylist ×1
ascii ×1
collections ×1
constructor ×1
exception ×1
export ×1
hashmap ×1
httprequest ×1
list ×1
phpmyadmin ×1
spring-mvc ×1
sql ×1
string ×1