JAVASPRING - 技术教程文章

java-Spring AMQP集成-用户手册确认【代码】

我正在测试具有Spring-Integration支持的Spring-AMQP,我已经进行了配置和测试:<rabbit:connection-factory id="connectionFactory" /> <rabbit:queue name="durableQ"/> <int:channel id="consumingChannel"><int:queue capacity="2"/> <!-- Message get Acked as-soon-as filled in Q --> </int:channel><int-amqp:inbound-channel-adapter channel="consumingChannel"queue-names="durableQ" connection-factory="connectionFac...

java-Spring MVC模拟【代码】

我正在尝试使用Spring MVC模拟模块对控制器进行单元测试.我添加了这个:@Beforepublic void beforeTest() {MockitoAnnotations.initMocks(this);standaloneSetup(new CarController());}效果很好.但是,我创建了一个控制器通知,该通知具有一个用@ExceptionHandler注释的方法.我想测试它在单元测试期间是否有效.我看到可以构建MockMvc对象并将其传递给standaloneSetup(..):MockMvcBuilders.standaloneSetup(new CarController()).se...

java-Spring不会注入JPARepository bean【代码】

道@Repository public interface LoginDao extends JpaRepository<Login, Integer> {Login findByLogin(String login); }验证器@Component public class PasswordChangeValidator implements Validator {private LoginDao loginDao;@Override public boolean supports(Class<?> aClass) {return PasswordChange.class.equals(aClass); }@Override public void validate(Object o, Errors errors) {PasswordChange passwordChange =...

java-Spring Batch Job的集成测试失败【代码】

好的,我知道从标题看这听起来很简单,但确实让我很困惑为什么会这样. 因此,我正在使用Spring Batch生成要使用Amazon的Simple Email Service发送的电子邮件.在我的CustomItemProcessor内部,我使用@Autowired正常连接我的AmazonEmailService服务. AmazonEmailService类实现了我的EmailSender接口. AmazonEmailService具有@Autowired AmazonSimpleEmailServiceClient,它用于实际调用Amazon Simple Email Service来执行操作. 我的Amazon...

java-SpringSecurity RememberMeServices不是通过注释注入的【代码】

我正在尝试配置SpringSecurity以与“记住我”身份验证一起使用. 这是我的Java配置:@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredUserDetailsService userDetailsService;@AutowiredDatabasePersistentTokeRepositoryImpl databasePersistentTokeRepositoryImpl;@Overrideprotected void configure(Authe...

java-Spring Tool Suite 3.6.1中的Hibernate Tools【代码】

我正在尝试在我的Spring Tool Suite 3.6.1版本中安装Hibernate Tools插件,安装完成没有任何错误,但是无法看到Hibernate透视图,也无法获得对Hibernate类进行反向工程的选项.我尝试了以下尝试,但没有成功:1.使用安装新软件选项…,更新站点链接为http://download.jboss.org/jbosstools/updates/development/luna/2.从Eclipse Marketplace安装Red Hat JBoss Developer Studio(Luna)8.0.0.CR13.从jboss开发人员工具站点下载zip存档更新...

java-Spring-boot应用程序中的套接字写入错误【代码】

我创建了一个spring-boot应用程序,并将其托管在Amazon EC2中. 问题似乎出在与MySQL数据库的正常连接上. 闲置一段时间后,发生以下错误,并且我的Web服务返回错误500. 我想知道是否可以添加一个配置属性来避免此错误.Caused by: org.hibernate.TransactionException: JDBC begin transaction failed: at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:76)at org.hibernate.engine.tr...

java-Spring Integration Inbound-Channel-Adapter逐行读取大文件【代码】

我目前正在将Spring Integration 4.1.0与Spring 4.1.2结合使用.我要求能够逐行读取文件并将读取的每一行都用作消息.基本上,我想允许我们的消息源之一“重播”,但是消息不是保存在单个文件中,而是保存在单个文件中.我对此用例没有交易要求.除了与JVM运行所在的服务器位于同一服务器上的文件外,我的要求与该发布类似:spring integration – read a remote file line by line 如我所见,我有以下选择: 1.使用int-file:inbound-chann...

java-Spring Framework和RestTemplate:无法使用REST服务【代码】

首先,我有两个Spring REST服务:服务A和服务B.服务A需要使用服务B公开的一些方法.A和B都是Spring @RestController. 服务A具有POST方法:@RequestMapping(value = "/mediumcandy/linkreachable", method = RequestMethod.POST)public ResponseEntity<ShortURL> shortenerIfReachable(@RequestParam("url") String url,@RequestParam(value = "sponsor", required = false) String sponsor,@RequestParam(value = "brand", required ...

java-Spring Data Rest:如何通过另一个对象的键进行搜索?【代码】

在Spring-Data-Rest中,对象的ID是随对象返回的URI.例如,如果我有一个用户,则它可能具有类似以下内容的URI:http://example.com/users/1在真实的REST api中,此URI是对象的ID,您不应仅使用“ 1”作为ID. 给出该答案,我该如何搜索属于该用户的所有订单?http://example.com/orders/search/findByUser?user={{XXX}}具体来说,{{XXX}}我该怎么用? 我知道我可以做相反的搜索:http://example.com/users/1/orders但就我而言,我需要搜索匹配...

java-Spring配置:2个具有相同类引用的bean【代码】

春季配置:我已经创建了2个Bean,它们引用的是同一类,但数据库会话不同.<bean id="abc1" class="abc"> <bean id="abc2" class="abc">现在有任何方法可以将ID为“ abc1”的bean设置为自动装配的默认bean,并在明确提及时使用abc2自动装配:@Autowiring @Qualifier("abc2") private abc obj;解:<bean id="abc1" class="abc" primary="true">使abc1默认为自动连线. 为了创建abc2的实例,我们可以使用以下命令:@Autowired @Qualifier("a...

java-Spring AMQP:MessageListener没有收到任何消息【代码】

目前,我正在Spring AMQP项目之上构建自定义库.我已经到达要实现消息侦听器以能够异步接收消息的地步.在进一步阅读了该项目指定的文档后,我管理层发现实现您自己的消息侦听器应该非常容易.只需实现MessageListener类并将其配置为在传入消息上触发即可. 这就是我所做的:public class ReceiveController implements MessageListener {@Overridepublic void onMessage(Message message){System.out.println("Received a message!");} ...

java-Spring RestController:拒绝具有未知字段的请求【代码】

我有以下端点:import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.POST;@RestController public class TestController {@RequestMapping(value = "/persons", method = POST, consumes = APPLICATION_JSON_VALUE, produces = A...

java-Spring Rabbit监听输出队列或接收【代码】

我正在开发具有Rabbitmq支持的应用程序.因此,我有一个消费者和一个生产者.我需要在两种方法之间做出决定,如何在两者之间建立通信. 第一种方式public void send(){//send to consumer and forgetrabbitTemplate.convertAndSend("","routing-key",my object);//waiting for output queue and messages from consumerwhile(true){//receive something.if(corellationID==what we need){//do what we needbreak;}} }第二种方式public v...

java-Spring Hibernate“您不能在托管事务中提交!”【代码】

将我的应用程序从JSF 1.2升级到JSF 2.1之后,尝试登录时出现以下Spring Hibernate异常:DEBUG,[interceptor.ExceptionInterceptor][],org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failedat org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java...

java-Spring-Data JPA:建模图形时删除边获得“列违反非空”【代码】

我有一组构建某种图形的实体.这由类Entity建模,其中两个字段对实体之间的关系进行建模.@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "fromId") private Set<EntityRelation> outEdges;@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "toId") private Set<EntityRelation> inEdges;保存时,所有outEdge都应属于该实体,inEdge是从这些实体“推断”的.问题是,在删除边缘后,我总是收到...

java-Spring JPA Projection包括链接【代码】

给定一个简单的Event模型,该模型具有一组Booking对象: 事件:@Entity public class Event {@Id@GeneratedValue(strategy=GenerationType.AUTO)private Long eventId;private Date start;private Date end;private String title;@OneToMany(mappedBy="event")private Set<Booking> Bookings;protected Event() {// for JPA}// Getters and setters omitted for brevity }预订:@Entity public class Booking { @Id@GeneratedValue(...

java-Spring JPA休眠重复【代码】

嗨,我是这个世界的新手.我正在使用Spring JPA Hibernate进行自己的应用程序.我有一个实体类.它可以正常工作,但可以复制属性.我的代码如下所示:import java.util.Date;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.validation.constraints...

java spring boot将字符串数组作为json中的参数传递给ajax调用【代码】

在我的应用程序中,我需要将一组参数从客户端传递到服务器端.我尝试了以下代码,但无法正常工作.我需要从复选框列表中获取数据并将其传递给服务器端.我的客户端代码$(".add").click(function(){monitoring.length=0;nonMonitoring.length=0;$('.modal-body input:checked').each(function() {monitoring.push($(this).val());});$('.addkeywords input:checked').each(function() {nonMonitoring.push($(this).val());});// alert(m...

java-Spring AOP-在调用setter之前获取旧的字段值【代码】

亲爱的所有人,我目前正在将Spring AOP(v4)和AspectJ与Load-Time-Weaver一起使用. 我目前正在寻找一种将脏标志机制添加到我的bean中的方法.因此,尽管我在使用bean的setter之前使用AOP来调用方法.我已经实现了,但是如何在修改之前访问旧的字段值?还是有一种获取字段名称的方法,以便我可以在调用setter之前先调用getter? 有人可以在这里为我提供一些示例,如何使切入点/建议看起来像参数一样传递?@Aspect public class MyAspect {@B...

java-Spring MVC @RestController->无论我做什么,都会在“ 400 Bad Request”中放入结果【代码】

我有一个Spring RestController,当我从中获取数据时,它工作得很好,但是当我尝试放入非常相同的数据时,我得到了400 Bad Request. 这是我的控制器仍然可以正常工作的最简单版本(我省略了GET方法):@RestController @RequestMapping("/configuration/ledstrips/{name}/display") public class DisplayController {@ResponseBody@RequestMapping(method = RequestMethod.PUT, produces = { "application/hal+json" })public DisplayRes...

java-Spring dataSource返回null【代码】

我正在尝试将MySQL数据库与Spring连接.我也想将DataSource setter方法与@Autowired一起使用.每次运行代码时,DataSource始终返回null. 错误:HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException有人可以告诉我我的代码有什么问题吗?请在下面找到我的代码. CustomerController.javapackage com.springapp.controller;import com.springapp.beans.Customer; import com.springapp.se...

java-Spring安全角色无法转换为授予的权限【代码】

User.javapublic class User implements Serializable{@Id@Size(min=5,max=15)@Column(name="username", unique=true)private String username;@OneToMany(mappedBy="user")private Collection<Role> roles;public User(User user) {this.username=user.username;this.roles=user.getRoles();} }角色.javapublic class Role implements Serializable{@Id@GeneratedValue(strategy=GenerationType.AUTO)private int id;private Stri...

java-Spring:检查给定的端点是否存在【代码】

就像标题一样.我想知道,是否有简单的方法来检查给定的路径是否可以转换为任何控制器中存在的(API)端点. 我有一个具有最高优先级的自定义过滤器,并且如果给定的请求不会产生任何结果(端点不存在),我想返回404状态代码.解决方法:看一下RequestMappingHandlerMapping类,尤其是它的getHandlerMethods方法. 从文档:public Map<T,HandlerMethod> getHandlerMethods() Return a (read-only) map with all mappings and HandlerMethod’s....

java-Spring无法使用空键返回JSON响应【代码】

以下是我编写的代码.@RequestMapping(value = "/getData", method = RequestMethod.GET) public @ResponseBody Map<String,String> test() throws IOException { Map<String,String> map = new HashMap<String,String>(); map.put("key","value"); map.put(null, "Key's Value"); //**This highlighted code causing the problem, if I remove this then it works fine.** return map; }当我点击URL localhost:8080 / ...

java-Spring Boot忽略application.yml中的数据源URL【代码】

我发现Spring Boot会默默忽略通过org.springframework.boot.autoconfigure.jdbc.DataSourceProperties加载的所有数据源属性. 我的application.yml包含:spring:datasource:name: this is not ignoredurl: this is ignored在启动过程中,我看到以下行:o.s.j.d.e.EmbeddedDatabaseFactory : Creating embedded database 'this is not ignored'调试显示DataSourceProperties对象在调用getUrl期间未设置任何属性(调试还显示抛出...

java-Spring Security Kerberos AD,校验和失败【代码】

我正在尝试使用http://docs.spring.io/spring-security-kerberos/docs/1.0.1.RELEASE/reference/htmlsingle/#samples-sec-server-win-auth中所述的带有Active Directory凭据的Spring Security Kerberos.我想说的是,我已经把大部分东西都弄下来了(SPN,密钥表等).现在我的校验和失败.假设我更改了主体名称,则会收到AES加密错误. 我在RHEL 6和Oracle Java 1.8 JCE上使用Spring Boot样本来自https://github.com/spring-projects/spring...

java-Spring Boot测试:测试通过,但不应通过(假肯定)【代码】

我有带有测试的Spring Boot项目,它不会失败(但应该). 我是在做错什么,还是Spring的问题? 作为一个小示例,我创建了一个小项目,其中包含2个实体(用户和类别)和一个具有DELETE方法(https://github.com/sk8ter/demo)的Controller类. 类别实体的ID为不带级联选项的用户实体,因此在删除具有类别的用户时应该失败:@Entity @Table(name = "user") public class User {@Id@GeneratedValueprivate long id;private String name;... }@Entit...

java-Spring Boot Security:使用自定义身份验证过滤器的异常处理【代码】

我正在使用Spring Boot Spring Security(java config).我的问题是旧的,但是我发现的所有信息都已部分过时,并且大部分包含xml-config(很难或什至不可能适应一段时间) 我正在尝试使用令牌(不存储在服务器端)进行无状态身份验证.长话短说-这是JSON Web令牌认证格式的简单模拟.我在默认一个之前使用了两个自定义过滤器: > TokenizedUsernamePasswordAuthenticationFilter,它在之后创建令牌在入口点(“ / myApp /登录”)成功认证> Toke...

java-Spring中的Bean创建异常【代码】

我正在尝试自动装配在另一个项目中声明的类,但是却遇到了运行时异常,而不是编译时异常.如果我删除了自动装配并使用新实例创建了一个对象,则该实例可以完美运行. 我收到以下错误:ErrorSEVERE: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getThreshold': Injection of autowired dependencies failed; nested exception is org.springframework.beans.fa...