简体   繁体   English

春季休养服务不起作用

[英]Spring Rest Service not working

在此处输入图片说明 I am new to spring and trying to develop a basic web service using spring(with maven). 我是spring的新手,正在尝试使用spring(with maven)开发基本的Web服务。 I am unable to make out whats the error: Here are the files I have: 我无法找出错误原因:这是我的文件:

web.xml (is under WEB-INF) web.xml(在WEB-INF下)

    <!DOCTYPE web-app PUBLIC  
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
 "http://java.sun.com/dtd/web-app_2_3.dtd" >  

<web-app>  
  <display-name>Archetype Created Web Application</display-name>  
  <servlet>  
 <servlet-name>springrest</servlet-name>  
 <servlet-class>  
  org.springframework.web.servlet.DispatcherServlet  
 </servlet-class>  
 <load-on-startup>1</load-on-startup>  
</servlet>  

<servlet-mapping>  
 <servlet-name>springrest</servlet-name>  
 <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app> 

springrest-servlet.xml(is under WEB-INF) springrest-servlet.xml(在WEB-INF下)

  <beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

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

CountryController.java CountryController.java

package com.sample.controller;

import java.util.ArrayList;
import java.util.List;

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;
import org.springframework.web.bind.annotation.RestController;

import com.sample.model.Country;  

@RestController  
public class CountryController {  

 @RequestMapping(value = "/countries", method = RequestMethod.GET,headers="Accept=application/json")  
 public List<Country> getCountries()  
 {  
  List<Country> listOfCountries = new ArrayList<Country>();  
  listOfCountries=createCountryList();  
  return listOfCountries;  
 }  

 @RequestMapping(value = "/country/{id}", method = RequestMethod.GET,headers="Accept=application/json")  
 public Country getCountryById(@PathVariable int id)  
 {  
  List<Country> listOfCountries = new ArrayList<Country>();  
  listOfCountries=createCountryList();  
  for (Country country: listOfCountries) {  
   if(country.getId()==id)  
    return country;  
  }  

  return null;  
 }  

// Utiliy method to create country list.  
 public List<Country> createCountryList()  
 {  
  Country indiaCountry=new Country(1, "India");  
  Country chinaCountry=new Country(4, "China");  
  Country nepalCountry=new Country(3, "Nepal");  
  Country bhutanCountry=new Country(2, "Bhutan");  


  List<Country> listOfCountries = new ArrayList<Country>();  
  listOfCountries.add(indiaCountry);  
  listOfCountries.add(chinaCountry);  
  listOfCountries.add(nepalCountry);  
  listOfCountries.add(bhutanCountry);  
  return listOfCountries;  
 }  
}  

Country.java Country.java

package com.sample.model;



public class Country {

    int id;
    String name;


    public Country(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

} }

I run the same on tomcat, and then , the response shows No Data Found. 我在tomcat上运行了相同的命令,然后,响应显示“未找到数据”。

First of all ur bas package must be somthing like this 首先,您的bas包必须像这样

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

and not 并不是

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

servlet映射是错误的也,代替<url-pattern>/</url-pattern>应该<url-pattern>/*</url-pattern>作为指定这里

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

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