问题

我正在从mongodb检索数据并以text / csv格式生成输出。
虽然用户记录对用户的字段兴趣没有任何价值,但是我仍在为给定用户的字段兴趣获取价值。

MyAnalysis:

在调试时,我得到了与mongodb数据匹配的结果,但是生成的text / csv数据是错误的。

59e75753a7986c0a86c758ffCOOKING
[COOKING]
59e759c6a7986c0a86c75901READING
[READING]
59e75f0ea7986c0c4b003e24READING
[READING]
59e7926ea7986c0fc809cbd9CRICKET
[CRICKET]



  任何人都可以指导我在做什么错。


以下是我编写的代码

@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private UserService userService;
@RequestMapping(value = "/endcustomer", method = RequestMethod.GET, produces = "text/csv")
public void endCustomerReport(HttpServletResponse response) {
    ICsvBeanWriter csvWriter=null;
    int pageSize=2000;
    Page<User> users=null;
    int page=0;
    String csvFileName = "End-customer.csv";
    List<String> headerList=null;
    Field[] declaredFields=null;
    UserReport userReport=new UserReport();
    try {
        response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", csvFileName));
        headerList = new ArrayList<>();
        declaredFields = UserReport.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            headerList.add(declaredField.getName());
        }
        csvWriter = new CsvBeanWriter(response.getWriter(),
                CsvPreference.STANDARD_PREFERENCE);

        String[] header = headerList.toArray(new String[]{});
        csvWriter.writeHeader(header);
/*   CsvPreference.STANDARD_PREFERENCE
    Ready to use configuration that should cover 99% of all usages.*/


        do {
            users = userService.getAllUsers(new PageRequest(page,pageSize));
            for (User user : users) {
                List interests=new ArrayList();
                userReport.setName(user.getName());
                userReport.setUsername(user.getUsername());
                userReport.setContact(user.getContact());
                userReport.setAddress(user.getAddress());
                userReport.setEmail(user.getEmail());
                userReport.setConnections(user.getConnections()); //See it's data
                userReport.setPending(user.getPending());//See it's data
                INTERESTS intrests[]=user.getInterests();
                if(intrests!=null) {
                    for (INTERESTS interest : intrests) {
                        System.out.println(user.getId()+""+interest);
                        interests.add(interest);
                    }
                    System.out.println(interests);
                    userReport.setInterests(interests);
                }
                //See it's data
                userReport.setOccupation(user.getOccupation());
                userReport.setGender(user.getGender());
                csvWriter.write(userReport, header);
            }
            page++;
        }while (users.hasNext());
    }
    catch (IOException e){
        e.printStackTrace();
    }
    finally{
        if(csvWriter!=null){
            try {
                csvWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


我的用户文件

{
"_id" : ObjectId("59e75753a7986c0a86c758ff"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164864",
"email" : "jalaj.chawla93@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj",
"dob" : ISODate("1993-02-10T18:30:00.000+0000"),
"username" : "jalaj",
"country" : "india",
"designation" : "engineer",
"imid" : "1",
"interests" : "COOKING"
}
{
"_id" : ObjectId("59e7583da7986c0a86c75900"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164865",
"email" : "jalaj.chawla95@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj5",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj5",
"country" : "india",
"designation" : "engineer",
"imid" : "2"
}
{
"_id" : ObjectId("59e759c6a7986c0a86c75901"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164866",
"email" : "jalaj.chawla96@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj6",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj6",
"country" : "india",
"designation" : "engineer",
"imid" : "3",
"interests" : "READING"
}
{
"_id" : ObjectId("59e75f0ea7986c0c4b003e24"),
"_class" : "com.ideationz.client.models.User",
"interests" : "READING",
"contact" : "9811164867",
"email" : "jalaj.chawla97@gmail.com",
"address" : "44HakikatNagar",
"gender" : "FEMALE",
"name" : "jalaj7",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj7",
"country" : "india",
"designation" : "engineer",
"imid" : "4"
}
{
"_id" : ObjectId("59e7926ea7986c0fc809cbd9"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164869",
"email" : "jalaj.chawla99@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj9",
"dob" : ISODate("1993-11-13T18:30:00.000+0000"),
"username" : "jalaj9",
"country" : "india",
"designation" : "engineer",
"imid" : "9",
"interests" : "CRICKET"
}


预期产量

name    username    contact     address      email                   connections     pending  interests   occupation    gender  region
jalaj   jalaj   9811164864  44HakikatNagar  jalaj.chawla93@gmail.com                            [COOKING]               MALE
jalaj5  jalaj5  9811164865  44HakikatNagar  jalaj.chawla95@gmail.com                            []                      MALE    //Change here
jalaj6  jalaj6  9811164866  44HakikatNagar  jalaj.chawla96@gmail.com                            [READING]               MALE
jalaj7  jalaj7  9811164867  44HakikatNagar  jalaj.chawla97@gmail.com                            [READING]             FEMALE
jalaj9  jalaj9  9811164869  44HakikatNagar  jalaj.chawla99@gmail.com                            [CRICKET]               MALE


显示的输出

name    username  contact   address email   connections pending interests   occupation  gender  region
jalaj   jalaj     9811164864    44HakikatNagar  jalaj.chawla93@gmail.com            [COOKING]       MALE
jalaj5  jalaj5    9811164865    44HakikatNagar  jalaj.chawla95@gmail.com            [COOKING]       MALE
jalaj6  jalaj6    9811164866    44HakikatNagar  jalaj.chawla96@gmail.com            [READING]       MALE
jalaj7  jalaj7    9811164867    44HakikatNagar  jalaj.chawla97@gmail.com            [READING]       FEMALE
jalaj9  jalaj9    9811164869    44HakikatNagar  jalaj.chawla99@gmail.com            [CRICKET]       MALE

最佳答案

该值保留上一次迭代中的值。

您必须在for循环中初始化userReportUserReport userReport=new UserReport();)或执行setInterests(null)

07-27 20:16
查看更多