我想在我的spring-boot应用程序开始监视目录以进行更改后运行代码.
我尝试过运行一个新线程,但此时@Autowired尚未设置服务.
我已经能够找到ApplicationPreparedEvent,在@Autowired注释设置之前触发.理想情况下,我希望在应用程序准备好处理http请求后触发该事件.
是否有更好的事件要使用,或者在应用程序在spring-boot中运行后运行代码的更好方法是什么?
我正在尝试向servlet发送POST请求.请求以这种方式通过jQuery发送:
var productCategory = new Object();
productCategory.idProductCategory = 1;
productCategory.description = "Descrizione2";
newCategory(productCategory);
Run Code Online (Sandbox Code Playgroud)
newCategory在哪里
function newCategory(productCategory)
{
$.postJSON("ajax/newproductcategory", productCategory, function(
idProductCategory)
{
console.debug("Inserted: " + idProductCategory);
});
}
Run Code Online (Sandbox Code Playgroud)
和postJSON是
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
Run Code Online (Sandbox Code Playgroud)
使用firebug,我看到JSON正确发送:
{"idProductCategory":1,"description":"Descrizione2"}
Run Code Online (Sandbox Code Playgroud)
但我得到415不支持的媒体类型.Spring mvc控制器有签名
@RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
@RequestBody ProductCategory productCategory)
Run Code Online (Sandbox Code Playgroud)
几天前它工作,现在不是.如果需要,我会显示更多代码.谢谢
我是Spring的新手,并开始从这个站点做官方指南:https: //spring.io/guides
我想做这个指南:https: //spring.io/guides/gs/scheduling-tasks/
我得到以下例外:
2014-02-14 16:25:21.614 INFO 9032 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springframework.scheduling.annotation.SchedulingConfiguration$$EnhancerByCGLIB$$5b48d763] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-02-14 16:25:21.638 INFO 9032 --- [ main] .c.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/work/Spring/SpringTutorial/target/classes/, file:/C:/work/apache-maven-3.0.3/repo/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter/1.0.0.RC1/spring-boot-starter-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot/1.0.0.RC1/spring-boot-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-autoconfigure/1.0.0.RC1/spring-boot-autoconfigure-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-logging/1.0.0.RC1/spring-boot-starter-logging-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jcl-over-slf4j/1.7.5/jcl-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jul-to-slf4j/1.7.5/jul-to-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/log4j-over-slf4j/1.7.5/log4j-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-core/1.0.13/logback-core-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-web/1.0.0.RC1/spring-boot-starter-web-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-tomcat/1.0.0.RC1/spring-boot-starter-tomcat-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-logging-juli/7.0.47/tomcat-embed-logging-juli-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-webmvc/4.0.0.RELEASE/spring-webmvc-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-databind/2.3.1/jackson-databind-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-annotations/2.3.0/jackson-annotations-2.3.0.jar, …Run Code Online (Sandbox Code Playgroud) 在我的控制器中,当我需要活动(登录)用户时,我正在执行以下操作来获取我的UserDetails实现:
User activeUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
log.debug(activeUser.getSomeCustomField());
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我认为Spring可以在这样的情况下让生活更轻松.有没有办法将UserDetails自动装配到控制器或方法?
例如,类似于:
public ModelAndView someRequestHandler(Principal principal) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,而不是得到UsernamePasswordAuthenticationToken,我得到了一个UserDetails?
我正在寻找一个优雅的解决方案.有任何想法吗?
Spring控制器是否可以处理这两种请求?
1) http://localhost:8080/submit/id/ID123432?logout=true
2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432
如果我定义了这种类型的单个控制器:
@RequestMapping (value = "/submit/id/{id}", method = RequestMethod.GET,
produces="text/xml")
public String showLoginWindow(@PathVariable("id") String id,
@RequestParam(value = "logout", required = false) String logout,
@RequestParam("name") String username,
@RequestParam("password") String password,
@ModelAttribute("submitModel") SubmitModel model,
BindingResult errors) throws LoginException {...}
Run Code Online (Sandbox Code Playgroud)
不接受带有"注销"的HTTP请求.
如果我定义两个控制器来分别处理每个请求,Spring会抱怨异常"已经有'Controller'bean方法...已映射".
我正在用弹簧靴做休息api.我需要使用输入参数(使用方法,例如GET,POST等),请求路径,查询字符串,此请求的相应类方法,以及此操作的响应,成功和错误来记录所有请求.
举个例子:
成功要求:
http://example.com/api/users/1
Run Code Online (Sandbox Code Playgroud)
日志应该看起来像这样:
{
HttpStatus: 200,
path: "api/users/1",
method: "GET",
clientIp: "0.0.0.0",
accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
method: "UsersController.getUser",
arguments: {
id: 1
},
response: {
user: {
id: 1,
username: "user123",
email: "user123@example.com"
}
},
exceptions: []
}
Run Code Online (Sandbox Code Playgroud)
或者请求错误:
http://example.com/api/users/9999
Run Code Online (Sandbox Code Playgroud)
日志应该是这样的:
{
HttpStatus: 404,
errorCode: 101,
path: "api/users/9999",
method: "GET",
clientIp: "0.0.0.0",
accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
method: "UsersController.getUser",
arguments: {
id: 9999
},
returns: {
},
exceptions: [
{
exception: "UserNotFoundException",
message: "User with id 9999 not found",
exceptionId: "adhaskldjaso98d7324kjh989",
stacktrace: ................... …Run Code Online (Sandbox Code Playgroud) 我正在学习在我的项目中使用的Spring Framework.我在web.xml文件中找到了 ContextLoaderListener条目.但无法弄清楚它对开发人员有何帮助?
在ContextLoaderListener的官方文档中,它说它是启动WebApplicationContext.关于WebApplicationContext JavaDocs说:
用于为Web应用程序提供配置的界面.
但我无法理解我在ContextLoaderListener中实现了什么,它在内部初始化WebApplicationContext?
根据我的理解,ContextLoaderListener 读取Spring配置文件(使用web.xml中的contextConfigLocation给出的值),解析它并加载该配置文件中定义的单例bean.类似地,当我们想要加载原型bean时,我们将使用相同的webapplication上下文来加载它.因此,我们使用ContextLoaderListener初始化web应用程序,以便我们提前读取/解析/验证配置文件,每当我们想要注入依赖项时,我们都可以毫不拖延地直接执行它.这种理解是否正确?
我是框架的新手(刚刚通过了这个类),这是我第一次使用springboot.
我正在尝试运行一个简单的Junit测试来查看我的CrudRepositories是否确实正常工作.
我一直得到的错误是:
无法找到@SpringBootConfiguration,您需要在测试java.lang.IllegalStateException中使用@ContextConfiguration或@SpringBootTest(classes = ...)
不弹簧启动配置自己?
我的测试班
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class JpaTest {
@Autowired
private AccountRepository repository;
@After
public void clearDb(){
repository.deleteAll();
}
@Test
public void createAccount(){
long id = 12;
Account u = new Account(id,"Tim Viz");
repository.save(u);
assertEquals(repository.findOne(id),u);
}
@Test
public void findAccountByUsername(){
long id = 12;
String username = "Tim Viz";
Account u = new Account(id,username);
repository.save(u);
assertEquals(repository.findByUsername(username),u);
}
Run Code Online (Sandbox Code Playgroud)
我的Spring启动应用启动器
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"domain.repositories"})
@ComponentScan(basePackages = {"controllers","domain"})
@EnableWebMvc
@PropertySources(value {@PropertySource("classpath:application.properties")})
@EntityScan(basePackages={"domain"})
public class Application …Run Code Online (Sandbox Code Playgroud) 我正在尝试建立一个大型REST服务服务器.我们使用的是Spring Boot 1.2.1 Spring 4.1.5和Java 8.我们的控制器正在实现@RestController和标准的@RequestMapping注释.
我的问题是Spring Boot为控制器异常设置了默认重定向/error.来自文档:
Spring Boot默认提供/错误映射,以合理的方式处理所有错误,并在servlet容器中注册为"全局"错误页面.
从使用Node.js编写REST应用程序多年来,对我来说,这对任何事情都是明智的.服务端点生成的任何异常都应在响应中返回.我无法理解为什么你会发送重定向到最有可能是Angular或JQuery SPA消费者的消费者,该消费者只是寻找答案而不能或不会对重定向采取任何行动.
我想要做的是设置一个全局错误处理程序,可以接受任何异常 - 有意地从请求映射方法抛出或由Spring自动生成(如果没有找到请求路径签名的处理程序方法,则为404),并返回标准格式化错误响应(400,500,503,404)到客户端没有任何MVC重定向.具体来说,我们将采用错误,使用UUID将其记录到NoSQL,然后使用JSON正文中日志条目的UUID向客户端返回正确的HTTP错误代码.
对于如何做到这一点,文档一直含糊不清.在我看来,你必须创建自己的ErrorController实现或以某种方式使用ControllerAdvice,但我看到的所有示例仍然包括将响应转发到某种错误映射,这没有帮助.其他示例表明,您必须列出要处理的每个Exception类型,而不是仅列出"Throwable"并获取所有内容.
任何人都可以告诉我我错过了什么,或指出我如何做到这一点的正确方向,而不建议Node.js更容易处理的链?
据说在关于DriverManagerDataSource类的Spring javadoc文章中,这个类非常简单并且推荐使用
使用容器提供的JNDI DataSource.这样的一个
DataSource可以DataSource在Spring ApplicationContext via中作为bean 公开JndiObjectFactoryBean
问题是:我该如何做到这一点?
例如,如果我希望有DataSourcebean来访问我的自定义MySQL数据库,那么我需要什么呢?我应该在上下文配置等中写什么?
spring ×10
java ×9
spring-boot ×4
spring-mvc ×2
ajax ×1
datasource ×1
jndi ×1
json ×1
junit ×1
logging ×1
post ×1
rest ×1
spring-data ×1
spring-rest ×1
tomcat ×1