小编Sur*_*h A的帖子

Spring mvc中的JSON数据绑定

我使用以下代码从我的Spring MVC控制器中的android应用程序接收了json数据.

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestHeader;

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String getMethod(@RequestHeader(value="json") String    
headerStr) {
System.out.println("POST");
System.out.println(headerStr);
return "hello";
}
Run Code Online (Sandbox Code Playgroud)

System.out.println(headerStr)的输出为

{"action":"check_login","password":"test","username":"test"}
Run Code Online (Sandbox Code Playgroud)

但我想将这个json数据绑定到下面的类.

public class LoginRequest {
private String action;
private String username;
private String password;

public String getAction() {
return action;
}

public void setAction(String action) {
this.action = action;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password; …
Run Code Online (Sandbox Code Playgroud)

java data-binding json spring-mvc

9
推荐指数
2
解决办法
4359
查看次数

处理大文本文件需要多长时间?

我有一个包含名字的大文本文件.

目标是生成随机名称(文件中的两个随机名称).

考虑到该文件有大约8k行,每行一个名称,尝试从该文件中获取随机行是否有效处理器?这是一个漫长的过程,因此在运行时有用吗?

我打算使用以下方法获取随机名称

 public static String choose(File f) throws FileNotFoundException
 {
 String result = null;
 Random rand = new Random();
 int n = 0;
 for(Scanner sc = new Scanner(f); sc.hasNext(); )
 {
    ++n;
    String line = sc.nextLine();
    if(rand.nextInt(n) == 0)
       result = line;         
 }

 return result;      
}
Run Code Online (Sandbox Code Playgroud)

对这段代码的新手分析让我觉得它在大约O(n)时间内运行

但是我用较少数量的字符串测试它(原始文件目前尚未完成,大约150个名称用于测试目的).

这是生成随机名称的有效方法吗?

@编辑

考虑到我想在很短的时间内生成名称,效率很重要.

java random algorithm big-o

1
推荐指数
1
解决办法
693
查看次数

标签 统计

java ×2

algorithm ×1

big-o ×1

data-binding ×1

json ×1

random ×1

spring-mvc ×1