我花了几天时间试图为我的Spring/JPA(Hibernate)集成测试找到一个有效的数据库连接,排除神秘的"没有发现数据库上下文"错误,我终于让它工作了,但我不明白我为什么要这样做做我做的.
请注意我的LocalContainerEntityManagerFacotryBean如何引用HibernateJpaVendorAdapter.
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="myEMF">
<property name="persistenceXmlLocation" value="file:src/test/resources/META-INF/persistence.xml" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter" ref="hibernateJpaAdapter" />
</bean>
<bean id="hibernateJpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
Run Code Online (Sandbox Code Playgroud)
为什么我用这个HibernateJpaVendorAdapter时,我的持久性单元已经配置了对Hibernate,如下图所示?
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<class>com.blah.blah.Class1</class>
<class>com.blah.blah.Class2</class>
<class>com.blah.blah.Class3</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1?zeroDateTimeBehavior=convertToNull"/>
<property name="hibernate.connection.username" value="uname"/>
<property name="hibernate.connection.password" value="pwd"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.ejb.event.post-insert"
value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-update"
value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-delete"
value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-update" …Run Code Online (Sandbox Code Playgroud) 考虑以下层次结构,其中实体WidgetA和WidgetB扩展抽象Widget超类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Widget implements Serializable {
@Column(name="serialNumber", length=64, nullable=false, unique=true)
private String serialNumber;
...
Run Code Online (Sandbox Code Playgroud)
和
@Entity
public class WidgetA extends Widget implements Serializable {
...
Run Code Online (Sandbox Code Playgroud)
和
@Entity
public class WidgetB extends Widget implements Serializable {
...
Run Code Online (Sandbox Code Playgroud)
我需要搜索Widgets serialNumber,但我不知道我在运行时搜索的Widget的具体类型.搜索小部件的正确方法是什么serialNumber,如果serialNumber是a的WidgetA,那么WidgetA返回的实例,依此类推?
我试图findyBySerialNumber()在WidgetDAO中使用a ,我收到一个错误,告诉我我无法实例化一个抽象类,这是有道理的,但我认为持久性提供程序会知道如何查看具体的子实体表和返回正确的实例.我可以这样做吗?
我正在使用"Spring Data JPA",因此Widget DAO非常简单:
public interface WidgetDAO extends JpaRepository<Widget, Long> {
public Widget findBySerialNumber(String serialNumber); …Run Code Online (Sandbox Code Playgroud) 假设我有以下三个类(为了简洁省略了getter和setter):
@JsonAutoDetect
public class InfoCollection{
private InfoType1 info1;
private InfoType2 info2;
}
@JsonAutoDetect
public class InfoType1{
private String fieldA;
}
@JsonAutoDetect
public class InfoType2{
private String fieldB;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个以这种格式JsonSerializer.serialize()序列化InfoCollection对象的函数:
{
"allInfo":{
"fieldA":"foo",
"fieldB":"bar"
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我现在拥有的:
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("allInfo");
jsonGenerator.writeObject(myInfoCollection.getInfo1());
jsonGenerator.writeObject(myInfoCollection.getInfo2());
jsonGenerator.writeEndObject();
Run Code Online (Sandbox Code Playgroud)
这导致以下异常:
org.codehaus.jackson.JsonGenerationException: Can not start an object, expecting field name
Run Code Online (Sandbox Code Playgroud)
我错过了一些小事情,还是我完全错误地采取了这种做法?
注:一对夫妇提出的解决方案迄今涉及编写的每一个人领域InfoType1和InfoType2.我正在寻找一个不需要这个的解决方案,因为我想在具有许多领域的大型类中使用该解决方案.
给定以下类结构:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {}
@Entity
public class Dog {}
@Entity
public class Cat {}
Run Code Online (Sandbox Code Playgroud)
使用Spring Data JPA,是否可以使用通用Animal存储库Animal在运行时持久保存而不知道Animal它是哪种类型?
我知道我可以使用每个实体的存储库并使用instanceof这样的方式来实现:
if (thisAnimal instanceof Dog)
dogRepository.save(thisAnimal);
else if (thisAnimal instanceof Cat)
catRepository.save(thisAnimal);
}
Run Code Online (Sandbox Code Playgroud)
但我不想诉诸于使用的不良做法instanceof.
我试过使用这样的通用存储库:
public interface AnimalRepository extends JpaRepository<Animal, Long> {}
Run Code Online (Sandbox Code Playgroud)
但这导致了这个例外:Not an managed type: class Animal.我猜是因为Animal不是Entity,它是一个MappedSuperclass.
什么是最好的解决方案?
顺便说一句 - Animal列出其余的我的课程persistence.xml,所以这不是问题.
我已将Spring依赖项升级到Spring 3.1.1.RELEASE,并且我正在尝试使用spring-test-mvc对一个简单的Controller进行单元测试.我一直在使用spring-test-mvc框架跟踪Spring REST Controller Test中使用的技术,因为它似乎对那个人起作用,但到目前为止我还没有成功.我认为我的测试上下文文件中缺少一些关键配置.
我没有错.我知道它不起作用的原因是因为Hello World永远不会被打印(参见控制器).我在这里错过了什么?
控制器:
@Controller
@RequestMapping("/debug")
public class DebugOutputController {
@RequestMapping(method = RequestMethod.POST)
public void saveDebugOutput(@RequestBody DebugOutput debugOutput, HttpServletResponse response) {
System.out.println("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
@RunWith(SpringJUnit4ClassRunner.class) //this lets tests access Spring beans defined in the context config file
@ContextConfiguration(locations={"file:src/test/resources/itest/restAPITestContext.xml"}) //tells the test where to get configuration and beans to be used by the test.
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) //overrides the default stack of listeners
public class ITRestAPI{
@Autowired
private …Run Code Online (Sandbox Code Playgroud) 我编写了一个RESTful Web服务,它只由设备使用,而不是浏览器.设备通过所有者现有的家用路由器访问互联网,并通过路由器每隔30秒发送一次HTTP请求与Web服务进行通信.这些请求主要是"轮询"请求,以查看Web服务是否具有该设备的任何新信息.
我想阻止任何ISP透明代理拦截请求并返回缓存的响应.我已经读过,这样做的一种方法是在请求的URL末尾附加一个随机查询字符串,以欺骗代理,使其认为它是一个唯一的请求.例如:
http://webservicedomain.com/poll/?randomNumber=384389
Run Code Online (Sandbox Code Playgroud)
我有能力做到这一点,但这是最好的方法吗?有点似乎是一个黑客.
我正在尝试理解过去几个月看到的 RDS“突发平衡”指标。根据 CloudWatch 中报告的指标,我的突发余额似乎从未低于 99%,但有时它会在数周内保持在 99% 附近,有时会很快恢复到 100%。下图是2020年5月1日至2020年7月23日之间的突发余额。
我很困惑,因为该指标与任何其他指标(例如负载均衡器报告的系统流量)之间似乎没有相关性。另外 - 为什么它永远不会低于 99%,为什么有时需要几周时间才能恢复到 100%,即使每天下午 5:00 以后我的系统上的流量很少,周末几乎没有?这是同一时间段内我的系统流量(负载均衡器请求计数):
当系统流量非常可预测且重复时,为什么会出现突发平衡?
我正在调查来自我的 Java/Spring 应用程序的约500 个(内部服务器错误)响应的根本原因。我可以localhost_access_log从这500 条回复中看到证据:
10.0.2.171 - - [20/Sep/2020:22:21:31 +0000] "POST /item HTTP/1.1" 500 5979
Run Code Online (Sandbox Code Playgroud)
但是当我查看catalina.out中相应的日志条目时,我没有看到任何堆栈跟踪或错误。日志记录就停止了:
2020-09-20 22:21:31.170 [http-nio-8080-exec-248] INFO c.a.e.w.w.s.c.QueueController - POST REQUEST to /item received
2020-09-20 22:21:31.170 [http-nio-8080-exec-248] INFO c.a.e.w.w.service.SnsTopicManager - Processing Request. ItemId=304513
Run Code Online (Sandbox Code Playgroud)
此后应该有额外的 INFO 级别日志记录,但显然出现了问题,因此出现了500响应。通常,500响应是由于未捕获的异常造成的,并且有一个随附的堆栈跟踪来解释该问题,但在本例中并非如此。
在没有记录堆栈跟踪或其他错误信息的情况下,如何在此处发生内部服务器错误?
我已指定以下存储桶策略来强制对 PUT 进行加密:
{
"Version": "2012-10-17",
"Id": "PutObjPolicy",
"Statement": [
{
"Sid": "DenyUnEncryptedObjectUploads",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::com.my.bucket/*",
"Condition": {
"StringNotEquals": {
"s3:x-amz-server-side-encryption": "aws:kms"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
和
上传的bucket对象的属性表明它们是加密的
然而
我的“存储桶属性”表明我没有使用默认加密:
我可以从控制台下载这些“加密”的图像并在下载后在本地查看它们,如果它们真的被加密,我希望这是不可能的。
那么......我是否实现了“静态加密”?
我正在使用Jackson 1.9.6(codehaus)在Spring MVC应用程序中对我的响应主体进行JSON序列化,而我却无法找到配置漂亮打印的方法.我能够找到的所有代码示例(比如this和this)都涉及到ObjectMapper或者实例化ObjectWriter,但我目前还没有使用其他任何实例化.我甚至不知道在哪里放这个代码.我所有的Jackson配置都是通过注释序列化为JSON的POJO来处理的.
有没有办法在注释中指定漂亮的打印?我认为他们会把它放在@JsonSerialize中,但它看起来不像.
我要序列化的类看起来像这样:
@JsonAutoDetect
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class JSONObject implements Serializable{...}
Run Code Online (Sandbox Code Playgroud)
我的Spring控制器方法如下所示:
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody List<Object> getMessagesAndUpdates(HttpServletRequest request, HttpServletResponse response) {
JSONObject jsonResponse = new JSONObject();
.
.
.
//this will generate a non-pretty-printed json response. I want it to be pretty-printed.
return jsonResponse;
}
Run Code Online (Sandbox Code Playgroud) 当我使用Application Load Balancer (ALB)将我的 Web 应用程序部署到 AWS 环境时,我的一些 Web 服务端点不会返回任何数据,并且我的 Chrome 浏览器会为一些 http 调用报告此错误:ERR_SPDY_PROTOCOL_ERROR
找到这个建议后,我在我的 ALB 配置中禁用了HTTP/2支持,现在一切正常。
为什么我必须在我的 ALB 中禁用HTTP/2?这里的根本问题是什么?我是否需要更改 Web 服务代码中的某些内容才能使用HTTP/2?
更新
以下是响应头:
HTTP/1.1 200
Date: Wed, 09 Jan 2019 21:39:13 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Locations Reached: Data to populate locations reached map
Run Code Online (Sandbox Code Playgroud)
正如下面的一个答案所建议的那样,罪魁祸首可能是我的一个标题(到达的位置)在名称中有一个空格,这是无效/格式错误的。我会确保空格被破折号取代。
load-balancing amazon-web-services amazon-elb http2 aws-application-load-balancer
这是我转换为Spring Data JPA 之前的查询。请注意我以前用来Hibernate.initialize()手动获取小部件的消息的方式。
public Object findWidget(final Widget findMe) {
Widget widget = getJpaTemplate().execute(new JpaCallback<Widget>() {
public Widget doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h where h.widgetId = ? ");
q.setParameter(1, findMe.getId());
Widget found = (Widget)q.getSingleResult();
//Initialize lazy associations
if(found!= null){
Hibernate.initialize(widget.getMessages());
}
return found;
}
});
return widget;
}
Run Code Online (Sandbox Code Playgroud)
这就是我的查询功能现在的样子。请注意,没有任何物体可以放入Hibernate.initialize()。
@Query("SELECT h FROM Widget h where h.widgetId = ?1 ")
public AccessPoint findWidget(String widgetId); …Run Code Online (Sandbox Code Playgroud) spring ×6
hibernate ×4
java ×3
jpa ×3
http ×2
jackson ×2
json ×2
amazon-elb ×1
amazon-rds ×1
amazon-s3 ×1
aws-application-load-balancer ×1
database ×1
encryption ×1
http2 ×1
inheritance ×1
pretty-print ×1
proxy ×1
spring-mvc ×1
spring-test ×1
tomcat ×1
unit-testing ×1
web-services ×1