我有一个生成端点如下:
public Book insertBook(Book book) {
PersistenceManager mgr = getPersistenceManager();
try {
if (containsShout(book)) {
throw new EntityExistsException("Object already exists");
}
mgr.makePersistent(book);
} finally {
mgr.close();
}
return book;
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何将错误返回给客户端.例如,这本书包含一些必填字段,ISNM检查等.
所以我会假设抛出一个异常,但是它如何映射到返回的json响应.json repsonse应包含所有字段错误以突出显示客户端中的这些字段.
NDB query() 是否通过缓存?
NDB 文档提到它将自动使用缓存。这仅适用于 get_by_id 还是也适用于查询语句,例如:
Product.query().order(Product.name).fetch()
Run Code Online (Sandbox Code Playgroud)
在许多页面中,我都显示了一个产品列表,这就是我希望它来自缓存的原因。
另一种方法是查询产品列表并自己存储在内存缓存中,但在这种情况下,产品更新不会最终出现在我的“手动”缓存中。或者可以选择自己将所有产品 ID 存储在缓存中,然后通过 memcache/datastore 检索它们。但这将需要一个 get_by_id 接受多个 id 并返回一个列表..
我正在尝试设置Spring Data/JPA DomainClassConverter以自动将(String)id转换为域类本身.
我的项目是使用Java Config(所以没有xml).
在我的WebConfig中,我目前有:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DomainClassConverter<DefaultFormattingConversionService>((DefaultFormattingConversionService) registry));
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎成功地连接了DomainClassConverter,因为我可以在打印时在转换服务中看到它:
ConversionService converters =
..<default converters>..
org.springframework.data.repository.support.DomainClassConverter@6ea4ce0d, org.springframework.core.convert.support.IdToEntityConverter@5d3f03b, org.springframework.core.convert.support.ObjectToObjectConverter@1d40b47a
Run Code Online (Sandbox Code Playgroud)
但是当提交嵌套表单(订单与客户参考)时,客户不会自动转换,因此我得到一个:
Failed to convert property value of type java.lang.String to required type org.mycomp.domain.Customer for property customer; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.mycomp.domain.Customer] for property customer: no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)
我想知道我在这里做错了什么?
我正在尝试将RESTEasy与Weld结合在AppEngine上,但是在构造函数注入方面遇到了麻烦.
我添加了RESTEasy CdiInjectorFactory上下文参数和Weld servlet侦听器.
我的RESTEasy应用程序类看起来像:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CustomerResource.class);
return classes;
}
}
Run Code Online (Sandbox Code Playgroud)
和CustomerResource:
@Path("/rest/app/customers")
public class CustomerResource {
private final CustomerService customerService;
@Inject
public CustomerResource(CustomerService customerService) {
this.customerService = customerService;
}
..
}
Run Code Online (Sandbox Code Playgroud)
CustomerService是一项简单的服务,例如:
public class CustomerService {
public List<Customer> getCustomers() {
..
}
}
Run Code Online (Sandbox Code Playgroud)
当我启动服务器时,我在日志中看到:
[INFO] 2014-01-30 21:34:53 INFO org.jboss.weld.Version:146 - WELD-000900: 2.1.2 (Final)
[INFO] 2014-01-30 21:34:53 WARN org.jboss.weld.environment.servlet.Listener:137 - @Resource injection not available in …Run Code Online (Sandbox Code Playgroud) 我有以下控制器方法:
@RequestMapping(method = GET, produces = APPLICATION_JSON_VALUE)
@Transactional(readOnly = true)
public ResponseEntity list(Pageable pageable, PagedResourcesAssembler pagedResourcesAssembler) {
Page<Customer> customers = customerRepository.findAll(pageable);
return ResponseEntity.ok().body(pagedResourcesAssembler.toResource(customers, customerResourceAssembler));
}
@RequestMapping(value = "/search", method = GET, produces = APPLICATION_JSON_VALUE)
@Transactional(readOnly = true)
public ResponseEntity search(@RequestParam("q") String q, Pageable pageable, PagedResourcesAssembler pagedResourcesAssembler) {
Specification spec = where(..some specs..);
Page<Customer> customers = customerRepository.findAll(spec, pageable);
return ResponseEntity.ok().body(pagedResourcesAssembler.toResource(customers, customerResourceAssembler));
}
Run Code Online (Sandbox Code Playgroud)
第一种方法将所有客户资源作为页面返回.
第二个也返回分页结果,但可以选择提供q要过滤的查询参数.
搜索方法的JSON HATEOAS响应包含下一页链接,如:
{
"rel": "next",
"href": "http://localhost:8080/api/customers/search?page=1&size=10{&sort}"
}
Run Code Online (Sandbox Code Playgroud)
问题是这里q查询参数丢失了.
我应该在PagedResourcesAssembler这里使用不同的吗?
我想创建一个部署在Google AppEngine基础架构上的Spring Boot应用程序.GAE目前仅支持servlet 2.5 Web应用程序.
是否可以将Spring Boot(使用自动配置)与旧式web.xml结合使用?
我可以使用一个指向@Configration类的contextClass/contextConfigLocation,包括@EnableAutoConfiguration吗?
所有Spring Boot示例似乎都使用带有main方法的简单Application类来运行应用程序.所以我想知道Spring Boot是否支持使用web.xml作为启动应用程序的起点?
我正在使用Spring MVC和Spring Data,并且还配置了Spring Data DomainClassConverter来自动将String id转换为适当的Domain类.
我现在使用标签实现Order to Customer参考:
<form:select path="customer">
<form:option value="" label="Select" />
<form:options items="${customers}" itemValue="id" />
</form:select>
Run Code Online (Sandbox Code Playgroud)
这导致给定的HTML:
<select id="customer" name="customer" class="span6">
<option value="">Select</option>
<option value="1">Customer A</option>
<option value="2">Customer B</option>
<option value="3">Customer C</option>
</select>
Run Code Online (Sandbox Code Playgroud)
当提交例如客户A的帖子时,我会得到一个例外:
org.apache.jasper.JasperException: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.ManyToOne nl.kapsalonreflection.domain.Customer for value ''; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!
Run Code Online (Sandbox Code Playgroud)
这根本没有意义,因为收到的价值应该是1而不是''.我还调试了收到的请求参数,它只包含customer = 1(如预期的那样) …
使用 Bootstrap 5 和 (s)css 时如何创建对角线风格的背景(如下面的屏幕截图所示)?
理想情况下与此处记录的其他 Bootstrap 后台实用程序完美配合: https ://getbootstrap.com/docs/5.3/utilities/background/
例如,当有:
<div class="bg-primary-subtle text-emphasis-primary">
Lorem ipsum.
</div>
Run Code Online (Sandbox Code Playgroud)
或使用其他颜色、渐变和不透明度:
<div class="bg-success text-white bg-gradient bg-opacity-75">
Lorem ipsum.
</div>
Run Code Online (Sandbox Code Playgroud)
能够轻松添加对角线效果(向上/向下任一方向)?
或者有时:
spring ×4
spring-data ×3
spring-mvc ×2
bootstrap-5 ×1
cdi ×1
css ×1
html ×1
java ×1
jboss-weld ×1
memcached ×1
python ×1
resteasy ×1
sass ×1
servlet-2.5 ×1
spring-boot ×1
weld ×1