春假休息不通电话

时间:2015-02-24 15:49:00

标签: rest

我写了一个非常小的休息服务。当我尝试通过其余的cilent时,我收到的错误是"方法不受支持"。任何人都可以就此提出建议。

**controller class**
@Controller
@RequestMapping("/movie")
public class MovieController {

    @RequestMapping(value="/{name}", method = RequestMethod.PUT, consumes="application/json")

    public @ResponseBody Student getMovie(@PathVariable String name, ModelMap model, @RequestBody Student student, HttpSession session) {

         Map<Integer, Student> empData = new HashMap<Integer, Student>();
         empData.put(1,  student);
         return student;

    }

}

**Request I am sending throught Rest DHC Client**

URL: http://localhost:8081/SpringMVC/movie/test   
method selected: PUT
Headers: Content-Type:application/json
Body: {
"userId":"21",
"firstName":"srinu",
"lastName":"nivas"
}

2 个答案:

答案 0 :(得分:0)

我只使用泽西岛做你想做的事。

我的web.xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>appName</display-name>
    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.appName.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/Pages/</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/(resources|(Pages))/.*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

在其他课程中,我返回一个Viewable:

我的休息班:

@GET
@Path("/PathToAccessPage")
@Produces(MediaType.TEXT_HTML)
public Viewable GetMyClassPage(@Context HttpServletRequest request, @Context UriInfo ui, @Context HttpHeaders hh) {
    try {           
        MyClass myClass = getMyClass(); 
        return new Viewable("/page", myClass);
    } 
    catch (Exception ex) {
        return new Viewable("/error", "Error processing request: " + ex.getMessage());
    }
}

希望你能帮忙!

答案 1 :(得分:0)

最后,我得到了解决方案。我没有导入以下两个罐子。

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

当我使用spring MVC时,正如你在上面的代码中看到的那样,我作为consumes = application / json给出,我认为springMVC会照顾它。这是我的错误,而且,我认为春天重新出现的错误应该是特定的。无论如何,得到了解决方案。谢谢大家的帮助。