0
votes

I encountered a problem when using spring mvc to implement restful api.

I use postman to test one method like this:

http://localhost:8080/Test/countryController/getAllCountries

  • Test is the project name
  • countryController is the value of @RequestMapping of controller class
  • getAllCountries is the get method

However, I got an error message:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I searched for many posts, but still can't solve it. Hope anyone can help me.

Here is the code below.

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context"
    xmlns:beans="http://www.springframework.org/schema/beans" 
 xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.2.xsd
         http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<annotation-driven />

<resources mapping="/resources/**" location="/resources/" />

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3306/country" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="12341234" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <!-- <beans:property name="annotatedClasses"> -->
    <!-- <beans:list> -->
    <!-- <beans:value>com.system.model.Country</beans:value> -->
    <!-- </beans:list> -->
    <!-- </beans:property> -->
    <beans:property name="packagesToScan">
        <beans:list>
            <beans:value>com.system.model</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

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

<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory"
        ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

web.xml

<!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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/country</url-pattern>
    </servlet-mapping>
</web-app>

Country.java

package com.system.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="country")
public class Country {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @Column(name="countryName")
    String countryName;

    @Column(name="population")
    int population;

    public Country() {
        super();
    }

    public Country(int i, String countryName, int population) {
        super();
        this.id = i;
        this.countryName = countryName;
        this.population = population;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public long getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}

CountryController.java

@RestController("country")
@RequestMapping(value="/countryController")
public class CountryController {

@Autowired
CountryService countryService;

@RequestMapping(value = "/getAllCountries", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Country> getCountries() {

    List<Country> listOfCountries = countryService.getAllCountries();
    return listOfCountries;
}

@RequestMapping(value = "/getCountry/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Country getCountryById(@PathVariable int id) {
    return countryService.getCountry(id);
}

@RequestMapping(value = "/addCountry", method = RequestMethod.POST, headers = "Accept=application/json")
public void addCountry(@RequestBody Country country) {
    countryService.addCountry(country);

}

@RequestMapping(value = "/updateCountry", method = RequestMethod.PUT, headers = "Accept=application/json")
public void updateCountry(@RequestBody Country country) {
    countryService.updateCountry(country);
}

@RequestMapping(value = "/deleteCountry/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteCountry(@PathVariable("id") int id) {
    countryService.deleteCountry(id);
}

}

Thanks.

1
Which URL you are calling? - Jens
When adding more information like your above comment, it's better to edit your question than to respond in a comment, as it makes it easier for people to see the full details of your question. I'd suggest editing to include the URL you are calling, and also to include what server your code is running on? - DaveyDaveDave

1 Answers

0
votes

Your servlet mapping is set to /country so the URL you should be using is:

http://localhost:8080/Test/country/countryController/getAllCountries