我正在尝试在Spring MVC中实现我的第一个REST样式控制器。
我遇到的问题是连接始终报告“转移已关闭”。我试图让第一个控制器方法工作,但它有问题,所以我尝试了一个简单的hello world示例,它仍然不起作用。
在Chrome中它会返回对象JSON,我可以在源视图中看到它,但连接不会发回200状态。
服务器是Tomcat只是基础安装。
我在这里做错了什么?
这是我从Chrome开发人员工具复制到简单的hello world控制器的curl命令。
curl 'http://localhost:8080/admin/regions/json/hello' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Cookie: sidebar_closed=0; ORA_EID_COOKIE_SUPPORT3_0=true; ORA_EID_COMPANY_ID3_0=10083; ORA_EID_ID3_0=7b4145537d746876476c492f524b4468334e526a556b435937694b482b51414a44615266724e6172573954506b7a43343d; ORA_EID_PASSWORD3_0=7b4145537d754b4856324b5345784d5779674e646271697476345a356330645650667967767a325262354450704850303d; ORA_EID_LOGIN3_0=61646d696e406f7261636c652e636f6d; ORA_EID_SCREEN_NAME3_0=7b4145537d7a4f30495a7836517057663136306e714a4a7363436b436242315835306938664f6279385072514d5476633d; ORA_EID_LFR_SESSION_STATE_10114=expired; JSESSIONID=9F89DCBCCFC0762CFA49FF2CAD4656CC' -H 'Connection: keep-alive' --compressed
curl: (18) transfer closed with outstanding read data remaining
控制器定义:
import com.ideafactory.mvc.general.search.SimpleSearchResults;
import com.ideafactory.mvc.locations.common.services.RegionService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;
/**
* The AJAX Controller handles the AJAX interface to the Region Administration service
*/
@RestController
@RequestMapping(value="/admin/regions/json/")
public class RegionAJAXController {
private static Logger logger = LogManager.getLogger(RegionAJAXController.class.getName());
@Autowired
private RegionService regionService;
@Autowired
private MessageSource messageSource;
@RequestMapping(value="/suburb_suggest/{searchTerm}", method= RequestMethod.GET)
public SimpleSearchResults suburbSuggest(@PathVariable String searchTerm)
{
logger.entry();
SimpleSearchResults results = regionService.getSuburbSuggestions(searchTerm);
logger.exit();
return results;
}
@RequestMapping(value="/hello")
public Greeting hello()
{
return new Greeting();
}
private class Greeting {
private String helloWorld = "Hello World";
public String getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
}
}
答案 0 :(得分:0)
您正在控制器中返回一个对象。要将对象转换为Json,您需要在类路径中使用Jackson库。