简体   繁体   中英

Java Spring Boot REST Service

Before I ask, apologies for my broken english.

Currently I develop using Spring Boot to make some REST Web Services. I use Maven, JPA and use Postgresql for the database. My problem is, when I run my spring boot , I saw there are no errors, it started normally; but when I try in my browser, I get errors :

This is what i get from browser

In my console on eclipse, I saw this log like error :

Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

Here is my project structure :

pom.xml :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath />
  </parent>

<dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <configuration>
                <ejbVersion>3.0</ejbVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

Application.java

package com.altoremittance.data;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@Configuration
@SpringBootApplication
@EnableJpaRepositories
@EnableAutoConfiguration
@ComponentScan("com.altoremittance.data.service")
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}

MemberController.java

package com.altoremittance.data.controller;

import java.util.List;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.altoremittance.service.MemberService;

import com.altoremittance.data.domain.Member;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value="/addMember", method=RequestMethod.GET,     produces="application/json")
    public @ResponseBody String getAllMember(){

        JSONObject jo = new JSONObject();

        jo.put("err_code", "00");

        return jo.toString();
    }

}

MemberRepository.java

package com.altoremittance.service;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.altoremittance.data.domain.Member;

public interface MemberRepository extends JpaRepository<Member, Long> {

}

MemberService.java package com.altoremittance.service;

import java.util.List;

import org.springframework.stereotype.Component;

import com.altoremittance.data.domain.Member;

public interface MemberService {

    public List<Member> findAll();

}

MemberServiceImpl.java

package com.altoremittance.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.altoremittance.data.domain.Member;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberRepository memberRepo;

    public List<Member> findAll(){
        return memberRepo.findAll();
    }

}

I've saw your attached image and I've saw that your are trying to access to http://localhost:8090/member/getAllMember ...

Why are you trying to access to getAllMember path if your controller @RequestMapping has been defined like addMember ?

Try to access to http://localhost:8090/member/addmember URL instead of the previous one.

Hope this helps.

Regards,

Finnally, i know the issue, i update in my Application.java

in the @ComponentScan, i change to @ComponentScan("com.altoremittance").

I refere this article : enter link description here

Thanks all.

将 ComponentScan 设置为@ComponentScan("com.altoremittance")

    you are firing wrong url i.e
    
    http://localhost:8090/member/getAllMember ( there is no "getAllMember" mapping on method level)
    
    It should be 
    
    http://localhost:8090/member/addMember ( please see, you have mentioned "addMember" mapping on method level)
    
    and also  remove extra annotation from your code , as per your code not required all.
    it is in proper package n all. Below code is sufficient to run your service.
    
    @SpringBootApplication
    public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    }

OR,

@ComponentScan("com.altoremittance.*")
@SpringBootApplication
  public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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