我正在使用eclipse Luna,tomcat 7和1.6。我正在为spring mvc控制器创建webservice。这是我的代码
package com.hp.hpl.ress.phoenix.web;
import com.hp.hpl.ress.phoenix.service.*;
import com.hp.hpl.ress.phoenix.domain.*;
import com.hp.hpl.ress.phoenix.flex.service.HomeViewService;
import com.hp.hpl.ress.phoenix.flex.service.NotificationFacade;
import com.hp.hpl.ress.phoenix.flex.vo.UserVO;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PhoenixController {
protected final Logger logger = Logger.getLogger(getClass().getName());
@Autowired
private InstallationService installationService;
@Autowired
private HomeViewService homeViewService;
@RequestMapping("/home.htm")
public ModelAndView showHomePage() {
List<Installation> allInstallations =
installationService.getAllInstallations();
ModelAndView mav = new ModelAndView("home");
mav.addObject("pageName", "Home Page");
mav.addObject("installations", allInstallations);
return mav;
}
@RequestMapping("/viewInstallation.htm")
public ModelAndView viewInstallation(@RequestParam("installationId") String
installationId) {
logger.debug("Started to render installation view: " + new Date());
Installation installation =
installationService.getInstallation(Integer.parseInt(installationId));
ModelAndView mav = new ModelAndView("viewInstallationDetail");
mav.addObject("pageName", "Installation detail");
mav.addObject("installation", installation);
logger.debug("Finished rendering installation view: " + new Date());
return mav;
}
@RequestMapping("/viewZone.htm")
public ModelAndView viewZone(@RequestParam("zoneId") String zoneId) {
logger.debug("Started to render zone view: " + new Date());
Zone zone = installationService.getZone(Integer.parseInt(zoneId));
ModelAndView mav = new ModelAndView("viewZoneDetail");
mav.addObject("pageName", "Zone detail");
mav.addObject("zone", zone);
logger.debug("Finished rendering zone view: " + new Date());
return mav;
}
@RequestMapping("/viewSensor.htm")
public ModelAndView viewSensor(@RequestParam("sensorId") String sensorId) {
logger.debug("Started to render sensor view: " + new Date());
Sensor sensor = installationService.getSensor(Integer.parseInt(sensorId));
Calendar start =
Calendar.getInstance(TimeZone.getTimeZone(sensor.getTimeZone()));
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
Calendar end = (GregorianCalendar)start.clone();
end.add(Calendar.DATE, 1);
List<SensorReading> readings =
installationService.getRangeOfReadings(sensor, start, end);
ModelAndView mav = new ModelAndView("viewSensorDetail");
mav.addObject("pageName", "Installation details");
mav.addObject("sensor", sensor);
mav.addObject("readings", readings);
logger.debug("Finished rendering sensor view: " + new Date());
return mav;
}
@RequestMapping("/homeSummary.htm")
public ModelAndView homeSummary() {
ModelAndView mav = new ModelAndView("viewHomeSummary");
//yyyy/MM/dd HH:mm
UserVO userSummary = homeViewService.getUserSummary(1, "2010/08/01 00:00",
3);
mav.addObject("UserSummary", userSummary);
return mav;
}
public void setInstallationService(InstallationService installationService)
{
this.installationService = installationService;
}
}
当我尝试使用eclipse为此控制器创建Web服务时,我得到的选择必须是WSDL错误..我尝试了很多次。我也在项目方面添加了轴2,
答案 0 :(得分:1)
我有同样的问题,我使用了这个解决方案。以这种方式使用spring controller作为服务:
@RequestMapping(value = "services/utente/getUtenteByUsername", method = RequestMethod.GET)
@ResponseBody
public String getUtenteDaUsername( @RequestParam("username") String username, Model model) {
utente = utenteBo.findByUsername(username);
String jsonResult = "";
if (utente != null) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
jsonResult = gson.toJson(utente);
return jsonResult;
}
else {
return null;
}
}
我已经使用json发送回复。 希望可以帮助你。