Java 什么';我的简单SpringWeb服务有什么问题?

Java 什么';我的简单SpringWeb服务有什么问题?,java,spring,spring-mvc,spring-web,Java,Spring,Spring Mvc,Spring Web,我构建了一个简单的SpringWeb应用程序。我有一个带有@RequestMapping的简单@Controller,但当我运行它时,我无法点击URL: http://localhost:8080/labutil/all 我做错了什么 package com.mycompany.ion.labutil.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowire

我构建了一个简单的SpringWeb应用程序。我有一个带有@RequestMapping的简单@Controller,但当我运行它时,我无法点击URL:

http://localhost:8080/labutil/all
我做错了什么

package com.mycompany.ion.labutil.controller;

import java.util.List;

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.RequestMethod;

import com.nokia.ion.labutil.service.LabService;

@Controller
public class LabController {

    @Autowired
    private LabService labService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String getAll() throws Exception {
        List<String> list = labService.getAll();

        // build fake little json formatted data
        StringBuffer sb = new StringBuffer("{");
        for (String s : list) {
            sb.append("{ "+s+" }, ");
        }
        sb.append("}");

        return sb.toString();
    }


}
package com.mycompany.ion.labutil.controller;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入com.nokia.ion.labutil.service.LabService;
@控制器
公共类LabController{
@自动连线
私人实验室服务实验室服务;
@RequestMapping(value=“/all”,method=RequestMethod.GET)
公共字符串getAll()引发异常{
List=labService.getAll();
//构建伪小json格式的数据
StringBuffer sb=新的StringBuffer(“{”);
用于(字符串s:列表){
sb.追加(“{”+s+“}”);
}
某人附加(“}”);
使某人返回字符串();
}
}

您必须将控制器注释为@RestController,或将@ResponseBody注释添加到方法中。这样,您就可以告诉Spring,该方法将对象作为HTTP主体响应返回。@RestController是一个方便的注释,同时带有@Controller和@ResponseBody注释

您为什么应该使用此注释的答案

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public String getAll() throws Exception {
    List<String> list = labService.getAll();

    // build fake little json formatted data
    StringBuffer sb = new StringBuffer("{");
    for (String s : list) {
        sb.append("{ "+s+" }, ");
    }
    sb.append("}");

    return sb.toString();
}
@RequestMapping(value=“/all”,method=RequestMethod.GET)
@应答器
公共字符串getAll()引发异常{
List=labService.getAll();
//构建伪小json格式的数据
StringBuffer sb=新的StringBuffer(“{”);
用于(字符串s:列表){
sb.追加(“{”+s+“}”);
}
某人附加(“}”);
使某人返回字符串();
}

另一方面,您应该返回一个对象,而不是经过解析的字符串Json,添加一些Json库,如Jackson或Gson,并使用相应的库视图实现配置视图。

您也可以尝试吗?对我来说,这似乎是一个url问题。请发布您的application-context.xml文件。您没有在控制器中的任何位置设置
/labutil
@Kael53建议应该有效(可能在URL中添加应用程序上下文),您需要的是
@RestController
,而不是
@Controller
。否则,返回的字符串应该是视图名。@Kael53/all也不起作用。该web应用正在以。。。当我去那里时,我看到index.jsp,它位于/src/main/webapp/index.jsp中。。。我假设URL是/labutil/all