【问题标题】:404 in Jersey/Struts applicationJersey/Struts 应用程序中的 404
【发布时间】:2017-01-17 19:06:17
【问题描述】:

我正在尝试使用 Jersey 为我的 struts 应用程序提供一个简单的 Web 服务。

当我调用客户端操作时,我收到以下错误

com.sun.jersey.api.client.UniformInterfaceException
Message: GET http://localhost:8080/shumer/rest/employee/get returned a response status of 404

web.xml 中的 servlet 声明

<servlet>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>spring.autowire</param-name>
        <param-value>byName</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

服务器资源

@Path("employee")
public class EmployeeResource {

    @Autowired
    EmpDao empDao;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Employee> get(@QueryParam("empCode") String empCode) throws Exception {

            EmpCriteria criteria = new EmpCriteria();
            criteria.setEmpCode(empCode);


            return empDao.searchByCondition(criteria);
        }

}

客户操作

public class EmployeeClientTestAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        Client client = Client.create();
        WebResource resource = client.resource("http://localhost:8080/shumer/rest/employee/get");

        String employees= resource.accept(MediaType.APPLICATION_JSON)
        .get(String.class);

        System.out.println(employees);

        request.setAttribute("employees", employees);

        return mapping.findForward("successful");
    }

}

我已经尝试过在有和没有/get 和资源url 结尾的情况下,以及在EmployeeResource @Path 注释中有和没有前导/ 的情况下。我的猜测是,为了让 Jersey servlet 处理它们,我必须在某个地方声明我的资源被涂在哪里,但我无法弄清楚。非常感谢您指出正确的方向。

编辑

我已将以下 init-param 添加到 servlet 元素中,但它仍然无法正常工作(这个包是我的资源类所在的位置)

<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>shumer.rest.resource</param-value>
</init-param>

【问题讨论】:

  • 您使用的是哪个版本的球衣?

标签: java jersey struts


【解决方案1】:

在get方法中写@Path("/get")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多