如果java.net.URL
在Spring Boot应用程序中使用,使用classpath
协议,它会按预期工作,因为Spring Boot注册URLStreamHandlerFactory
.例如new URL("classpath: someFile.whatever")
.
但是当执行此代码时,java.net.MalformedURLException: unknown protocol: classpath
将抛出JUnit测试异常.
URLStreamHandlerFactory
在为JUnit测试初始化Spring上下文时,似乎没有注册适当的.
重现步骤:
1)创建Spring Boot Starter项目(例如,仅使用入门Web).
2)添加test.json
文件src/main/resources
3)添加以下bean:
@Component
public class MyComponent {
public MyComponent() throws MalformedURLException {
URL testJson = new URL("classpath: test.json");
System.out.println(testJson);
}
}
Run Code Online (Sandbox Code Playgroud)
4)启动应用程序作为Java应用程序工作正常
5)运行默认的"contextLoads"测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringUrlTestApplicationTests {
@Test
public void contextLoads() {
}
}
Run Code Online (Sandbox Code Playgroud)
java.net.MalformedURLException: unknown protocol: classpath
抛出异常.
在JUnit测试中使用URL与classpath资源的合适方法是什么?
在真实的用例中,我无法改变new URL("classpath: test.json")
它,因为它来自第三方库.
试图复制test.json
中src/test/resources
,以测试是否错误可能由缺少的资源造成的-没有成功.
我正在尝试编写一个简单的Spring AOP应用程序,但我遇到了xml配置问题.
我的xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop">
<bean id="audience" class="springaop.Audience">
</bean>
<bean id="sam" class="springaop.Singer">
<property name="id" value="1"></property>
</bean>
<aop:config>
<aop:aspect ref="audience">
<aop:before pointcut="* springaop.Singer.perform(..)"
method="takeSeats"></aop:before>
</aop:aspect>
</aop:config>
</beans>
Run Code Online (Sandbox Code Playgroud)
我得到这个警告和例外:
WARNING: Ignored XML validation warning org.xml.sax.SAXParseException: SchemaLocation: schemaLocation value = 'http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop' must have even number of URI's. Exception: Line 18 in XML document from class path resource [aop-conf.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration …
我试图从spring网站运行这个例子: 教程 除了Spring Boot部分.
在web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.evgeni.websock.WebSocketConfig
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
Java配置:
@Configuration
@ComponentScan(basePackages = {"com.evgeni.controller"})
@EnableWebSocketMessageBroker
@EnableWebMvc
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketMessageBrokerConfigurer {
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
public void configureClientInboundChannel(ChannelRegistration registration) {
// TODO Auto-generated method stub
}
public void configureClientOutboundChannel(ChannelRegistration registration) {
// TODO Auto-generated method stub
}
public void configureMessageBroker(MessageBrokerRegistry registry) { …
Run Code Online (Sandbox Code Playgroud) 我设法用Spring 4和Stomp创建简单的Websocket应用程序.在这里查看我的上一个问题 然后我尝试使用远程消息代理(ActiveMQ).我刚开始经纪人并改变了
registry.enableSimpleBroker("/topic");
Run Code Online (Sandbox Code Playgroud)
至
registry.enableStompBrokerRelay("/topic");
Run Code Online (Sandbox Code Playgroud)
它起作用了.
问题是如何配置代理?据我所知,在这种情况下,应用程序自动发现localhost上的代理:defaultport,如果我需要将应用程序指向其他机器上的其他代理,该怎么办?
如果您知道父 ID,如何使用 Spring data JPA 存储库保存子实体?
例如,如果我们有一对多关系:
@Entity
public class Customer {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@ManyToOne
private CustomerCategory category;
}
@Entity
public class CustomerCategory {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
人们可以做这样的事情:
CustomerCategory someCategory = customerRepository.findOne(1L);// how to skip that line. The id should be enough.
Customer cust = new Customer("Evgeni", "Dimitrov", someCategory);
customerRepository.save(cust);
Run Code Online (Sandbox Code Playgroud)
JPA 可以做到load
(只需创建代理并设置 Id)而不是“获取”(从数据库中选择)。Spring Data JPA 可以实现这一点吗?
我将创建将在数据库上运行的类.该类将具有addRecord(),getAllRecords()等函数.我正在寻找一种设计课程的好方法.我应该:1)为每个功能创建新的连接.像这样:
void readRecords(){
try {
Connection con = DriverManager.getConnection (connectionURL);
Statement stmt = con.createStatement();
ResultSet rs = stmd.executeQuery("select moviename, releasedate from movies");
while (rs.next())
System.out.println("Name= " + rs.getString("moviename") + " Date= " + rs.getString("releasedate");
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
con.close();
}
}
Run Code Online (Sandbox Code Playgroud)
要么
2)最好将一个连接作为一个memeber变量
class MyClass{
private Connection con;
public MyClass(){
con = DriverManager.getConnection (connectionURL);
}
}
Run Code Online (Sandbox Code Playgroud)
并为每个函数创建语句.
3)或别的......
我尝试使用2个视图解析器:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.evgeni.dfr.controller" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="cache" value="false" />
<property name="viewClass" value="com.evgeni.drf.faces.FacesView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".xhtml" />
<property name="order" value="1" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="0" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
应用程序始终只使用最低顺序而不是另一个.在目前的情况下,如果我的控制器返回"someView",The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.
即使有"pages/someView.xhtml" ,应用程序也会响应.
春季版 - 3.2.3
编辑:如果我在控制器中有2个方法,方法A返回"viewA",方法B返回"viewB".我们在'views'文件夹中有viewA.jsp,在'pages'中有viewB.xhtml.
Case1:UrlBasedViewResolver - > …
我试图制作一个简单的Spring Boot Web应用程序:
POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
主要课程:
@ComponentScan(basePackages={"controller"})
@Import(MvcConfig.class)
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
@RequestMapping("/helloView")
public String helloView(){
return "homeView";
}
}
Run Code Online (Sandbox Code Playgroud)
此外,根据src/main
我得到
src
-main
-resources
applicaion.properties
-webapp
-WEB-INF
-jsp
-homeView.jsp
Run Code Online (Sandbox Code Playgroud)
在application.properties我得到:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: …
Run Code Online (Sandbox Code Playgroud) 我有一个Java类(MyResponse
)由多个RestController方法返回,并有很多字段.
@RequestMapping(value = "offering", method=RequestMethod.POST)
public ResponseEntity<MyResponse> postOffering(...) {}
@RequestMapping(value = "someOtherMethod", method=RequestMethod.POST)
public ResponseEntity<MyResponse> someOtherMethod(...) {}
Run Code Online (Sandbox Code Playgroud)
我想忽略(例如,不序列化)一个方法的属性之一.
我不想忽略该类的空字段,因为它可能对其他字段有副作用.
@JsonInclude(Include.NON_NULL)
public class MyResponse { ... }
Run Code Online (Sandbox Code Playgroud)
该JsonView看起来不错,但据我了解我必须标注在类的所有其他领域有@JsonView
不同之处,我想忽略这听起来笨拙的人.如果有办法做"反向JsonView"这样的话会很棒.
关于如何忽略控制器方法的属性的任何想法?
我有Spring-boot-starter-remote-shell的Spring Boot应用程序.当我把这个hello.groovy脚本打印出'hello'时就可以了.
package commands
import org.crsh.cli.Usage
import org.crsh.cli.Command
class hello {
@Usage("Say Hello")
@Command
def main(InvocationContext context) {
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试注入一些Spring bean时,它总是为null.
package commands
import org.crsh.cli.Usage
import org.crsh.cli.Command
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.batch.core.launch.JobLauncher
@Component
class hello {
@Autowired
JobLauncher jobLauncher;
@Usage("Say Hello")
@Command
def main(InvocationContext context) {
if(jobLauncher != null){
return "OK";
}else{
return "NULL";
}
return "hello j";
}
}
Run Code Online (Sandbox Code Playgroud)
我有 @ComponentScan(basePackages={"com....", "commands"})
spring ×9
java ×8
spring-mvc ×5
spring-boot ×2
websocket ×2
facelets ×1
groovy ×1
hibernate ×1
jackson ×1
jdbc ×1
jpa ×1
json ×1
jsp ×1
junit ×1
oop ×1
spring-4 ×1
spring-data ×1
spring-test ×1
xml ×1