小编szx*_*nyc的帖子

终止mvn spring-boot:run不会停止tomcat

我可以成功启动spring-boot mvn spring-boot,文档提到优雅地退出应用程序命中ctrl-c.

Terminate batch job (Y/N)? Y
Run Code Online (Sandbox Code Playgroud)

maven进程终止,但Tomcat仍在运行,我仍然可以访问网页.当我尝试再次启动spring-boot时,它无法启动Tomcat,因为端口正在使用中.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.1.0.BUILD-SNAPSHOT)

2014-05-02 …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat maven spring-boot

42
推荐指数
5
解决办法
7万
查看次数

Spring-Boot MVC模板未加载(未找到404)

我在这里有一个非常简单的Spring-Boot MVC应用程序无法正常工作.一个控制器,其中一页未加载并且未找到404.

我在控制器方法中放置了一个System.out.println("Home Page")语句,并验证它是否已映射并正确触发但模板未加载.

错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).
Run Code Online (Sandbox Code Playgroud)

文件夹结构:

src/main/java
 +-Application.java
 +-WebController.java

src/main/resources
 +-templates
   +-index.html
Run Code Online (Sandbox Code Playgroud)

缩略控制台输出:

Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*] …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

31
推荐指数
1
解决办法
7万
查看次数

mvn spring-boot:run不会启动spring

解答:我将版本标签从0.0.1-SNAPSHOT更改为1.0.2.RELEASE并且它有效,请参阅下面的答案.

我按照此文档并按照说明创建了Example.java.当我跑mvn spring-boot:run春天不开始它只是说BUILD SUCCESS.我的理解是Spring应该启动并且Tomcat提供页面.

E:\workspace\SpringBoot>mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.1.0.BUILD-SNAPSHOT:run (default-cli) @ myproject >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace\SpringBoot\src\main\resources
[INFO] skip non existing resourceDirectory E:\workspace\SpringBoot\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myproject ---
[INFO] …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

20
推荐指数
3
解决办法
6万
查看次数

Spring CrudRepository .orElseThrow()

如果数据库查询返回空,抛出异常的正确方法是什么?我正在尝试使用该.orElseThrow()方法,但它不会编译:

Meeting meeting = meetingRepository.findByMeetingId(meetingId).orElseThrow(new MeetingDoesNotExistException(meetingId));
Run Code Online (Sandbox Code Playgroud)

编译器说:

"他的方法orElseThrow(Supplier)中的Optional类型不适用于参数(MeetingRestController.MeetingDoesNotExistException)

是否可以使用lambda表达式执行此操作?

CrudRepository:

import java.util.Optional;

import org.springframework.data.repository.CrudRepository;

public interface MeetingRepository extends CrudRepository<Meeting, Long>{
    Optional<Meeting> findByMeetingId(Long id);
}
Run Code Online (Sandbox Code Playgroud)

例外:

@ResponseStatus(HttpStatus.CONFLICT) // 409
class MeetingDoesNotExistException extends RuntimeException{
  public MeetingDoesNotExistException(long meetingId){
    super("Meeting " + meetingId + " does not exist.");
  }
}
Run Code Online (Sandbox Code Playgroud)

java spring crud java-8 spring-boot

11
推荐指数
1
解决办法
2万
查看次数

Spring Data Rest PUT vs PATCH LinkableResources

我正在使用Spring Data REST来公开我的实体及其关系.我在两个实体之间有一个OneToOne关系,我正在尝试更新/改变与PUT和PATCH的关系.

我注意到Spring Data REST只允许您更新链接资源 - JPA映射实体(OneToMany,ManyToOne等),它们也是AggregateRoots(有一个存储库) - 通过PATCH并被PUT忽略 .

这可以在LinkedAssociationSkippingAssociationHandler类中看到:

if (associationLinks.isLinkableAssociation(association)) {
  return;
}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?这背后的原因是什么?

是因为设计要求我们将关联视为资源本身,如文档的这一部分所示?我可以通过带有Content-Type text/uri-list的PUT改变关系,但感觉不自然,需要额外的HTTP请求.

spring spring-data spring-data-jpa spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
663
查看次数

在Spring-Boot/Security中使用@AuthenticationPrincipal

我根据参考文档设置了我的Spring Security应用程序,经过数小时的故障排除后,我继续将@AuthenticationPrincipal传递给我的控制器.

身份验证机制对我的数据库中的用户工作正常,但仍然是null @AuthenticationPrincipal.我咨询了几家网上的帖子,包括这个这个,但还是我得到空.

我使用的是Spring-Boot 1.2.2和Spring Security 3.2.6.

相关POM:

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

应用:

@SpringBootApplication
public class AuditWebApplication {
  // code
}
Run Code Online (Sandbox Code Playgroud)

WebSecurityConfigurerAdapter:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(prePostEnabled=true)
@EnableWebMvcSecurity
public class SecurityConfiguration WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .httpBasic()
            .and()
            .logout()
            .logoutSuccessUrl("/#/login")
            .and()
            .authorizeRequests()
            .antMatchers("/index.html", "/views/**", // public side. …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security spring-boot spring-web

5
推荐指数
2
解决办法
8054
查看次数

Spring Boot 将验证代码映射到 MessageSource 消息

问题:

我正在尝试使用尽可能多的 Spring Boot Auto-configuration 来减少样板代码。我无法让 Spring 验证代码自动映射到我的外部化 messages.properties。只有当我添加自己的LocalValidatorFactoryBean并使用完全限定的 javax 约束消息时,它才会起作用。

目标

我想重写defaultMessage@ javax.validation.constraints.NotNull全球在我的应用程序基于Spring输入验证码秒。

我做的第一件事就是注册这个 bean...我真的必须注册吗?我看到 Spring Boot 有一个,但它似乎与 messages.properties 无关。

@Bean
public LocalValidatorFactoryBean validator(MessageSource messageSource) {
  LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
  bean.setValidationMessageSource(messageSource);
  return bean;
}
Run Code Online (Sandbox Code Playgroud)

下一个让我很惊讶。Spring Validation 有这个漂亮的DefaultMessageCodesResolver,它为看起来像这样的失败提供了出色的代码策略:

"code": "NotNull",
"codes": [
   "NotNull.user.firstName",
   "NotNull.firstName",
   "NotNull.java.lang.String",
   "NotNull"
],
Run Code Online (Sandbox Code Playgroud)

但是这些代码甚至不考虑用于@NotNull 约束。验证转换的 spring 文档暗示这样的东西确实存在,但还没有任何有用的东西。

我在这里编写了一个示例应用程序,您可以看到它不起作用。

如何根据messages.properties 中的代码覆盖Spring Validation 错误的默认消息?

相关的build.gradle

  • springBootVersion = '2.0.0.RELEASE'
  • 编译('org.springframework.boot:spring-boot-starter-web')

src/main/resources/messages.properties:

NotNull.user.firstName=This doesn't work
NotNull.firstName=Or …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-boot spring-web

5
推荐指数
1
解决办法
4646
查看次数

Apache使用Spring-Boot加载两次加载?

我正在使用Spring-Boot 1.2.7.RELEASE和Apache Ignite 1.4.0进行原型设计,并注意到一些奇怪的东西,也许它只是一个日志记录配置.

看起来Apache Ignite试图用Spring开始两次?如果我注释掉SpringApplication.run行,那么它看起来只会启动一次.也许我没有正确使用Spring和Apache Ignite?

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>IgniteDemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <ignite.version>1.4.0</ignite.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-core</artifactId>
            <version>${ignite.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-spring</artifactId>
            <version>${ignite.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

IgniteDemoApplication

package com.example;

import org.apache.ignite.Ignite;
import …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot ignite

4
推荐指数
2
解决办法
2993
查看次数

Apache ProxyPass 负载平衡所有 URL

使用 Apache HTTP Server,如何对所有 URL 进行负载平衡,而不仅仅是根“/”或根下的一些子目录(如“/css”),而是绝对平衡后端的所有内容?

我的配置只会路由文字根 URL“/”,但不涵盖其他 URL,我需要使用正则表达式吗?

相关配置:

<Proxy balancer://mycluster>
    BalancerMember https://server1:8443
    BalancerMember https://server2:8443
    ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost _default_:443>
    SSLProxyEngine on
    ProxyPass "/" "balancer://mycluster"
    ProxyPassReverse "/" "balancer://mycluster"
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

Apache access_log(注意 /css 收到 500 响应):

172.18.0.1 - - [10/May/2017:20:22:55 +0000] "GET / HTTP/1.1" 200 196
172.18.0.1 - - [10/May/2017:20:22:58 +0000] "GET /css HTTP/1.1" 500 528
Run Code Online (Sandbox Code Playgroud)

阿帕奇错误日志:

[Wed May 10 20:22:58.607433 2017] [proxy:warn] [pid 9:tid 140682836559616] [client 172.18.0.1:35304] AH01144: No protocol handler was valid for the URL /css. If you …
Run Code Online (Sandbox Code Playgroud)

apache http httpd.conf httpserver

4
推荐指数
1
解决办法
1274
查看次数

通过命令行在类路径(包括JARS)中搜索或列出Java类

我真的很喜欢javap命令行程序来反编译和检查类,但是大多数时候我无法重新收集类的标准包名称:

javap java.nio.file.Files
Run Code Online (Sandbox Code Playgroud)

如果我不知道软件包名称,那么我会使用Google。是否有内置的Java程序或实用的Linux命令可以搜索并列出给定类名的所有匹配包?

java linux

3
推荐指数
1
解决办法
3669
查看次数

普罗米修斯距离向量联合

我有两个范围向量(命中率和未命中数),我想按它们的类型进行汇总。有些类型有命中率,其他类型有命中率,有些兼有。这是我试图求和的两个独立指标,但所得向量没有意义。它缺少了一些价值,我认为这是因为它们要么全命中,要么全命中。我这样做是完全错误的方式吗?

sum by (type) (increase(metric_hit{}[24h]) + sum by (type) (increase(metric_miss{}[24h])
Run Code Online (Sandbox Code Playgroud)

monitoring metrics grafana prometheus

3
推荐指数
1
解决办法
1572
查看次数

找不到 spring-boot 基本 JSP 404

无法使用 spring-boot 加载非常简单的 JSP 页面,出现 404 Not Found。

src/main/java/SampleWebJspApplication.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleWebJspApplication extends SpringBootServletInitializer {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebJspApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

src/main/java/WebController.java

@Controller
public class WebController {
    @RequestMapping("/")
    public String welcome() {
        return "welcome";
    }
}
Run Code Online (Sandbox Code Playgroud)

src/main/webapp/WEB-INF/jsp/welcome.jsp

<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
    <body>
        <h1>Hello.</h1>      
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

即使调试器显示“欢迎”正在从控制器 RequestMapping 返回,也会得到 404。

白标错误页面

此应用程序没有明确的 /error 映射,因此您将其视为后备。

美国东部时间 2015 年 3 月 7 日星期六 19:35:15 出现意外错误(类型 = 未找到,状态 = 404)。

java spring jsp spring-mvc spring-boot

2
推荐指数
1
解决办法
7758
查看次数