![](/img/trans.png)
[英]RESTful web service creation tutorial with Java, Eclipse. Apache Tomcat, not working
[英]Web Service creation in eclipse
我正在使用Eclipse Luna,tomcat 7和axis 1.6。 我正在為Spring MVC控制器創建Web服務。 這是我的代碼
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,
我遇到了同樣的問題,並且我使用了此解決方案。 通過這種方式將spring控制器用作服務:
@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發送響應。 希望可以為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.