今天,前端测试出现一个bug,postman和浏览器测试接口正常,页面测试json格式有一堆斜杠,经过仔细的debug发现了这个bug的原因。

出现bug的原因是两个:一个是前端页面发送的请求和postman发送请求的区别是,前端发送请求中多了"Accept: application/json"这一项,第二个原因是添加了Jackson依赖后,会自动向Spring中增加一个jsonHttpMessageConverter。这两个原因共同导致了这个bug。
具体来说,在使用了@ResponseBody之后,Spring判断使用哪一种HttpMessageConverter的机制是这样的:
首先遍历请求头中 Accept: 后面所有的类型,所以遍历的第一个类型就是application/json。
其次遍历所有的HttpMessageConverter,只要遍历出符合application/json的HttpMessageConverter,直接使用此HttpMessageConverter,不管你Response中具体的格式是什么。

把Jackson这个依赖注解掉后,问题解决,当然应该有自定义HttpMessageConverter的方法去解决这个问题。
做了一些简单的算法练习题。正确答案都看不懂。
1.找到一个数组中出现了奇数次的那个唯一的数。
package august18;
import java.util.HashMap;
public class FindOdd {
public static void main(String[] args) {
int [] arr = {1,2,1};
System.out.println(findItBest(arr));
}
public static int findIt(int[] A) {
HashMap<Integer,Integer> map = new HashMap<>();
for (int i : A){
if (!map.containsKey(i)){
map.put(i, 1);
}
else {
map.put(i, map.get(i) + 1);
}
}
for (int i : map.keySet()){
if (map.get(i) % 2 != 0){
return i;
}
}
return 0;
}
public static int findItBest(int[] A) {
int xor = 0;
for (int i = 0; i < A.length; i++) {
xor = xor ^ A[i];
//xor ^= A[i];
}
return xor;
}
}
2.一个数字问题,附两个标准答案。
persistenceBest2(39) == 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
persistenceBest2(999) == 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
persistenceBest2(4) == 0 // because 4 is already a one-digit number
package august18;
/**
* Created by ZengTian on 2017/8/18.
* Write a function, persistenceBest2, that takes in a positive parameter num and returns its multiplicative persistenceBest2, which is the number of times you must multiply the digits in num until you reach a single digit.
For example:
persistenceBest2(39) == 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
persistenceBest2(999) == 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
persistenceBest2(4) == 0 // because 4 is already a one-digit number
*/
public class PersistentBugger {
public static void main(String[] args) {
System.out.println(persistence(999L));
}
private static int count = 0;
private static int persistence(long n){
count = 0;
calculation(n);
return count;
}
private static int calculation(long n) {
if (n < 10){
String s = String.valueOf(n);
return Integer.parseInt(s);
}
count ++;
return calculation(middle(n));
}
private static long middle(long n){
if (n < 1){
return 1;
}
long left = n / 10;
long right = n - (n / 10) * 10;
return middle(left) * right;
}
//标准答案
public static int persistenceBest(long n) {
long m = 1, r = n;
if (r / 10 == 0)
return 0;
for (r = n; r != 0; r /= 10)
m *= r % 10;
return persistenceBest(m) + 1;
}
public static int persistenceBest2(long n) {
int times = 0;
while (n >= 10) {
n = Long.toString(n).chars().reduce(1, (r, i) -> r * (i - '0'));
times++;
}
return times;
}
}
3.排队问题,前面数组是队伍的人需要的时间,后面数字代表有几个柜台,计算一共多少时间。这个和标准答案一样。
package august18;
import java.util.Collections;
import java.util.PriorityQueue;
/**
* Created by ZengTian on 2017/8/18.
* There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
The function has two input variables:
customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
The function should return an integer, the total time required.
EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:
There is only ONE queue, and
The order of the queue NEVER changes, and
Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
The diagram on the wiki page I linked to at the bottom of the description may be useful.
So, for example:
queueTime([5,3,4], 1)
// should return 12
// because when n=1, the total time is just the sum of the times
queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the
// queue finish before the 1st person has finished.
queueTime([2,3,10], 2)
// should return 12
N.B. You should assume that all the test input will be valid, as specified above.
P.S. The situation in this kata can be likened to the more-computer-science-related idea of a thread pool, with relation to running multiple processes at the same time:
https://en.wikipedia.org/wiki/Thread_pool*/
public class TheSupermarketQueue {
public static void main(String[] args) {
int [] arr = {2,3,10};
System.out.println(solveSuperMarketQueue(arr, 2));
}
public static int solveSuperMarketQueue(int[] customers, int n) {
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++){
queue.add(0);
}
for (int i : customers){
int p = queue.poll();
queue.add(p + i);
}
return Collections.max(queue);
}
}
明天:修改接口
问题:无
总结:无
进度:http://task.ptteng.com/zentao/project-burn-277.html
demo时间:大约延期10天。
评论