Java Spring引导REST服务
在我开口之前,我为我的蹩脚英语道歉 目前,我使用Java Spring引导REST服务,java,spring,maven,spring-rest,Java,Spring,Maven,Spring Rest,在我开口之前,我为我的蹩脚英语道歉 目前,我使用springboot开发了一些restweb服务。我使用Maven、JPA和Postgresql作为数据库。 我的问题是,当我运行我的spring boot时,我看到没有错误,它正常启动;但当我在浏览器中尝试时,会出现以下错误: 在eclipse上的控制台中,我看到以下类似日志的错误: Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.u
springboot
开发了一些restweb服务。我使用Maven、JPA和Postgresql作为数据库。
我的问题是,当我运行我的spring boot
时,我看到没有错误,它正常启动;但当我在浏览器中尝试时,会出现以下错误:
在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)
MemberController.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);
}
}
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();
}
}
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> {
}
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();
}
}
MemberRepository.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);
}
}
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();
}
}
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> {
}
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();
}
}
package com.altoremitance.service;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Repository;
导入com.altoremitance.data.domain.Member;
公共接口MemberRepository扩展了JpaRepository{
}
MemberService.java
包com.altoremitance.service
import java.util.List;
import org.springframework.stereotype.Component;
import com.altoremittance.data.domain.Member;
public interface MemberService {
public List<Member> findAll();
}
import java.util.List;
导入org.springframework.stereotype.Component;
导入com.altoremitance.data.domain.Member;
公共接口成员服务{
公共列表findAll();
}
MemberServiceImpl.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);
}
}
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();
}
}
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> {
}
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();
}
}
package com.altoremitance.service;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Service;
导入org.springframework.transaction.annotation.Transactional;
导入com.altoremitance.data.domain.Member;
@服务
@交易的
公共类MemberServiceImpl实现MemberService{
@自动连线
私人会员库会员回购;
公共列表findAll(){
返回memberRepo.findAll();
}
}
我看到了您的,并且我看到您正在尝试访问http://localhost:8090/member/getAllMember
如果控制器@RequestMapping
的定义类似于addMember
,为什么要尝试访问getAllMember
路径
尝试访问http://localhost:8090/member/addmember
URL,而不是上一个URL
希望这有帮助
您好,我看到了您的,并且我看到您正在尝试访问http://localhost:8090/member/getAllMember
如果控制器@RequestMapping
的定义类似于addMember
,为什么要尝试访问getAllMember
路径
尝试访问http://localhost:8090/member/addmember
URL,而不是上一个URL
希望这有帮助
关于,最后,我知道这个问题,我在我的应用程序中进行了更新
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);
}
}
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();
}
}
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> {
}
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();
}
}
在@ComponentScan中,我改为@ComponentScan(“com.altoremitance”)
我指的是这篇文章:
谢谢大家。最后,我知道这个问题,我在我的应用程序中进行了更新。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);
}
}
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();
}
}
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> {
}
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();
}
}
在@ComponentScan中,我改为@ComponentScan(“com.altoremitance”)
我指的是这篇文章:
谢谢大家。将ComponentScan设置为
@ComponentScan(“com.altoremitance”)
将ComponentScan设置为@ComponentScan(“com.altoremitance”)
作为错误状态,您是否尝试添加了带有/error的请求映射?您好,谢谢您的回复,您的意思是否与MemberController.java中的类似:@RequestMapping(value=“/error”,method=RequestMethod.GET)public@ResponseBody String displayError(){return“error”;}作为错误状态,您是否尝试使用/error添加请求映射?您好,Hardik,感谢您的回复,您的意思是像MemberController.java中这样:@RequestMapping(value=“/error”,method=RequestMethod.GET)public@ResponseBody String displayError(){return“error”}谢谢您的回复。我已经更新了这个,但仍然不起作用。嗨,彼得,你不需要在你的代码中更新任何东西。。仅访问http://localhost:8090/member/addmember
URL而不是上一个。。。你能提供你的请愿书的新截图吗?感谢您的回复。我已经更新了这个,但仍然不起作用。嗨,彼得,你不需要在你的代码中更新任何东西。。仅访问http://localhost:8090/member/addmember
URL而不是上一个。。。你能提供你的请愿书的新截图吗?关于你为什么将@Configuration
,@EnableAutoconfiguration
等注释与@SpringBootApplication
放在一起?阅读以了解@SpringBootApplication
与使用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注释相同。当然,我现在已将其删除…谢谢@jcgarcia.)为什么要将@Configuration
、@EnableAutoconfiguration
等注释与@SpringBootApplication
放在一起?阅读以了解@SpringBootApplication
与使用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注释相同。当然,我现在已将其删除…谢谢@jcgarcia.)