简体   繁体   English

REST Web服务不起作用

[英]REST Web Service not working

I am just trying to create a simple test web service. 我只是想创建一个简单的测试Web服务。 I want to access the User in XML format at the following url: http://localhost:8080/Online_Shopping/dispatcher/rest/hello , but when I go to the URL nothing is displayed. 我想通过以下URL以XML格式访问用户: http:// localhost:8080 / Online_Shopping / dispatcher / rest / hello ,但是当我转到URL时,没有任何显示。

Here is my code for the service: 这是我的服务代码:

package com.shopping.controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.shopping.model.User;
@Path("/rest")
public class RESTController {

    @GET
    @Path("/hello")
    @Produces(MediaType.APPLICATION_XML)
    public User getUser() {
        return new User("paymon","123",true);
    }

}

The following code is in my spring-config.xml 下面的代码是在我的春天-config.xml中

<context:component-scan base-package="com.shopping.controller" />

This is in my web.xml 这是我的web.xml

<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-config.xml</param-value>
    </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/dispatcher/*</url-pattern>
    </servlet-mapping>

Your application is running on port 8080 , so to access this URL you need to add in your servlet dispatcher the "Online_Shopping" 您的应用程序在端口8080上运行,因此要访问此URL,您需要在servlet调度程序中添加“ Online_Shopping”

   <servlet-mapping>
       <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>Online_Shopping/dispatcher/*</url-pattern>
    </servlet-mapping>

or Just access your api without Online_Shoppping 或无需Online_Shoppping即可访问您的api

You have to use Spring MVC's annotations instead and use @Controller or @RestController to annotate you controller to be registered as a spring controller. 您必须改为使用Spring MVC的注释,并使用@Controller或@RestController注释您的控制器以注册为spring控制器。

Try this : 尝试这个 :

@RestController
@RequestMapping(value = "/rest")
public class RESTController {

    @RequestMapping(value = "/hello", 
    method = RequestMethod.GET, 
    produces = "application/xml")
    public User getUser() {
        return new User("paymon","123",true);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM