Spring rest Web服务控制器无法返回List对象

时间:2016-04-18 22:19:09

标签: java spring list rest controller

感谢您查看我所面临的问题以及您的回复。

问题 - > Spring休息控制器无法返回List对象。从休息客户端浏览器扩展发出请求我没有收到任何错误但我只能看到等待动画图标而没有响应。我等了几分钟然后中止了请求,多次这样做了.Checked日志并基于它观察到响应即将到来,直到控制器似乎与控制器中的return语句有关。

代码如下: -

Spring rest controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wells.exptrack.constants.Result;
import com.wells.exptrack.service.UserService;

@Controller
@RequestMapping(value="/rest/service")
public class EmployeeController {
@Autowired
UserService userService;
@RequestMapping(value="/get/all/News/user/{empId}", method=RequestMethod.GET,consumes = {"application/json"}, produces = "application/json")
public @ResponseBody Result getAllNews(@PathVariable("empId") int eId) {        
    return userService.getAllNews(eId);
    }   }

服务类如下:

@Override
@Transactional
public Result getAllNews(int empId) {                       
com.myorg.myproj.entities.Employee employeeEntity = userDao.getEmpById(empId);  
List cards= employeeEntity.getCards();   
com.myorg.myproj.entities.Card cardEntity = (Card) cards.get(0);        
List allNews = cardEntity.getNews();                
Result result = new Result();   
NewsPayload tran = new NewsPayload();
tran.setNewsList(allNews);
result.setPayload(tran);
return result;
}

其他支持类:

public class Result {   
private int code;
private String description;
private Payload payload;    
public int getCode() {
    return code;
}
public void setCode(int code) {
    this.code = code;
}
public Payload getPayload() {
    return payload;
}
public void setPayload(Payload payload) {
    this.payload = payload;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
}


public class NewsPayload implements Payload{
private int empId;
public List getNewsList() {
    return newsList;
}
public void setNewsList(List newsList) {
    this.newsList = newsList;
}
private  List newsList = new ArrayList();
public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}
}

0 个答案:

没有答案