Tag: spring boot

Spring Boot:为RestControllers配置url前缀

我不是Spring专家,而且它是黑盒子,即使使用Spring的文档,我也很难自己解决问题。 有时,我只是不知道我正在寻找什么才能开始我的搜索…… 在我的Spring Boot应用程序中,我试图弄清楚如何为我的所有RestControllers配置一个唯一的url前缀。 我真正想要的就是从根上下文“/”中提供我的静态内容,但是可以从不同的上下文访问我的RestController端点,比如“/ api / *”。 我知道如何通过application.properties更改应用程序的默认上下文,但这不是我想要的。 当谈到servlet,映射等时,我在这里表现出我的无知,因为我说我正试图为两种不同类型的内容获得两种不同的上下文。

我需要Spring 5 for Java 9吗?

Spring 5被提升为“支持JDK 9”,同时Spring 4.3.13的文档声明它支持Java 8(以及6和7) 。 这是否意味着我需要在可以之前升级到Spring 5 在JRE 9中运行我的应用程序? 使用JDK 9编译我的应用程序?

添加新的Spring会话时,cookie值出错

在我的基于Spring Boot 1.4的应用程序中,我使用Spring Session将会话数据与JDBC一起存储在数据库中。 这适用于默认会话。 但是当我想添加一个新会话时(通过向应用程序URL添加?_s=1 ),我得到以下exception: java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 这是什么问题? 编辑:请注意,我自己没有设置cookie值, Spring Session这样做的。 因此我无法分辨它试图设置的值。 完整的堆栈跟踪在这里: java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:160) ~[tomcat-embed-core-8.5.4.jar:8.5.4] at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:109) ~[tomcat-embed-core-8.5.4.jar:8.5.4] at org.apache.catalina.connector.Response.generateCookieString(Response.java:989) ~[tomcat-embed-core-8.5.4.jar:8.5.4] at org.apache.catalina.connector.Response.addCookie(Response.java:937) ~[tomcat-embed-core-8.5.4.jar:8.5.4] at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386) ~[tomcat-embed-core-8.5.4.jar:8.5.4] at org.springframework.session.web.http.DefaultCookieSerializer.writeCookieValue(DefaultCookieSerializer.java:112) ~[spring-session-1.2.1.RELEASE.jar:na] at org.springframework.session.web.http.CookieHttpSessionStrategy.onNewSession(CookieHttpSessionStrategy.java:213) […]

带@ EnableJpa *注释的Spring @WebMvcTest

我有以下主类。 @EnableJpaAuditing @SpringBootApplication @EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class) public class SampleApplication { public static void main(String[] args) { SpringApplication.run(SampleApplication.class, args); } } 以及unit testing类。 @RunWith(SpringRunner.class) @WebMvcTest(controllers = { AccountController.class }, secure = false) public class AccountControllerTest { @Autowired private MockMvc mockMvc; // Commented out below 如果我要运行unit testing,它会给我这个错误。 IllegalArgumentException:必须至少存在一个JPA元模型! 所以我研究了一下,发现它是由于@EnableJpa*注释在使用@WebMvcTest被置于主类中,因此它找不到@Entity类,因此出现错误消息。 我遇到了一个页面 ,并描述了我或多或少相同的问题,并建议使用@SpringBootTest和@AutoConfigureMockMvc而不是@WebMvcTest 。 这是唯一可行的方法吗? 我希望能够使用@WebMvcTest因为使用@SpringBootTest会加载完整的上下文,并且需要更长的时间来运行测试。 按要求包括测试类。 @WebMvcTest(controllers = […]

如何在没有setter的情况下绑定请求参数?

我有一个带有GET处理程序的简单控制器,它接受一个绑定请求参数的对象: @RestController @RequestMapping(“/test”) public class SampleController { @GetMapping public SomeResponse find(RequestParams params) { // some code } } RequestParams是一个简单的POJO类: public class RequestParams { private String param1; private String param2; // constructor, getter, and setters } Everthing工作正常,但我想摆脱设置者使对象不可变为公共使用。 在针对Spring 5.0.2的@RequestMapping处理程序方法的文档中,我们读到可能的有效方法参数是: 命令或表单对象将请求参数绑定到bean属性(通过setter) 或直接绑定到字段 是否有可能以某种方式覆盖默认的Spring Boot配置,以便使用reflection而不是使用setter将请求参数绑定到类属性? 更新2018年 在Spring的文档的更高版本中,引用的语句已被重新定义,不再包含有关绑定请求参数直接到字段的信息。

启动后立即启动Spring启动应用程序

我正在尝试构建一个简单的SpringBoot应用程序。 当我运行我的spring启动应用程序时,它在启动后立即关闭,下面是控制台日志: . ____ _ __ _ _ /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| […]

Spring-Boot:如何在@ImportResource中引用application.properties

我的Spring Boot应用程序中有一个applicationContext.xml文件。 在此文件中,它有一个属性占位符 – $ {profile.services.url} – 用于配置 bean的“address”属性。 在我的Application.java类中,我导入了这个文件。 @ImportResource(“classpath:applicationContext.xml”) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 我在application.properties中定义了“profile.services.url”。 但是,在我的XML文件中构建bean时无法识别它。 我尝试添加以下内容,但它似乎不起作用。 关于如何让@ImportResource识别Spring Boot的属性支持的任何建议?

在Apache Camel应用程序中,unit testing如何注入模拟端点来代替真实端点?

我正在使用Apache Camel实现消息转换器模式 ,以使用来自RESTful端点的消息并将它们发送到AMQP端点。 封闭的应用程序基于Spring Boot,因此我使用Camel的“ spring-boot ”组件来集成这两个框架。 正如这个spring-boot链接中的文档所示,我在@Configuration -annotated类中实现了我的Camel路由,该类扩展了RouteBuilder : @Component public class MyRestToAmqpRouter extends RouteBuilder { @Override public void configure() throws Exception { from(“jetty:http://my-restful-url”) .process(exchange -> { // convert the message body from JSON to XML, take some // incoming header values and put them in the outgoing // body, etc… }).to(“rabbitmq://my-rabbitmq-url”); } } […]

无法在IntelliJ Idea中启动spring-boot应用程序

概观 我可以使用spring-boot的maven插件运行应用程序,但不能使用IDE运行它。 以下几行详细介绍了我的程序。 应用程序类如下: package com.blss.retailServices.dataExportRouter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan @EnableAutoConfiguration(exclude = { VelocityAutoConfiguration.class }) public class DataExportRouterApplication { public static void main(String[] args) throws Exception { SpringApplication.run(DataExportRouterApplication.class, args); } } exception堆栈跟踪如下: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due […]

如何使用hibernate在spring boot中实现分页

我正在使用带有hibernate的spring boot,我想在我的项目中使用分页。 我在谷歌搜索并看到了很多例子,但我无法在我的项目中实现它。 我希望如果我在我的url中传递1,那么应该有10个结果,如果我传递2,那么接下来的10个结果应该会出现,依此类推。 这是我的道 @Transactional public interface PostDao extends CrudRepository{ @Query(getAllPostsByRank) List getAllPostsByRank(); final String getAllPostsByRank= “from Post order by value DESC”; } 这是我的控制器 @RequestMapping(“/top”) @ResponseBody public List getAllPosts(HttpServletRequest req, HttpServletResponse res) throws ServletException { List postobj = postDao.getAllPostsByRank(); return postobj; } 这是我的url: http://localhost:8888/v1.0/post/top/1 请建议。