有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaServletSpring+jax-rs服务

我是spring services的新手,我正在尝试创建一个简单的Web服务示例,返回字符串Hello world。我的代码配置是:

<servlet>
    <servlet-name>myServletName</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.MyPackageDirectionWebServices.remoting</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MyServletName</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</servlet-mapping>

你好。爪哇

package com.MyPackageDirectionWebServices.remoting;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller

@RequestMapping("/hello")

public class HelloWs {
    @RequestMapping(value= "helloWorld", method = RequestMethod.GET)
    @ResponseBody

    public String HelloWorld() {
        return "Hello World";
    }

在我的AppContext远程处理中。我在后面添加了xml

<mvc:annotation-driven />
<context:component-scan base-package="com.MyPackageDirectionServices.remoting" />

但当我启动应用程序并转到服务时:

localhost:8080/MyAppname/rest/hello/helloWorld

我得到HTTP 404,并在Eclipse控制台中发现此错误:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/MyAppname/rest/hello/helloWorld] in DispatcherServlet with name 'MyServletName

有人能帮我拿这个吗?太多了


共 (1) 个答案

  1. # 1 楼答案

    Toni,您需要在contextConfigLocation中提到spring配置xml文件名,如下所示(根据需要更改路径)

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/AppContext-Remoting.xml</param-value>
        </init-param>
    </servlet>
    

    如果您有java配置(而不是xml文件),您可以以不同的方式使用contextClass和contextConfigLocation