简体   繁体   English

Java Spring Boot REST 服务

[英]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.目前我使用Spring Boot开发一些 REST Web 服务。 I use Maven, JPA and use Postgresql for the database.我使用 Maven、JPA 并使用 Postgresql 作为数据库。 My problem is, when I run my spring boot , I saw there are no errors, it started normally;我的问题是,当我运行spring boot ,我看到没有错误,它正常启动; 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 :在我的 eclipse 控制台中,我看到了这个类似错误的日志:

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 : 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应用程序.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成员控制器.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 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; MemberService.java包 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 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 ...我看到了您的附加图片,并且看到您正在尝试访问http://localhost:8090/member/getAllMember ...

Why are you trying to access to getAllMember path if your controller @RequestMapping has been defined like addMember ?如果您的控制器@RequestMapping已像addMember一样定义,您为什么要尝试访问getAllMember路径?

Try to access to http://localhost:8090/member/addmember URL instead of the previous one.尝试访问http://localhost:8090/member/addmember URL 而不是之前的 URL。

Hope this helps.希望这会有所帮助。

Regards,问候,

Finnally, i know the issue, i update in my Application.java最后,我知道这个问题,我在我的Application.java 中更新

in the @ComponentScan, i change to @ComponentScan("com.altoremittance").在@ComponentScan 中,我更改为@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);
    }
    
    }

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

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