-
关于JAVA Entity entity = entityClass.getAnnotation(Entity.class);
2015-08-06 23:19:31关于JAVA Entity entity = entityClass.getAnnotation(Entity.class); 2013-08-31 15:50匿名 | 浏览 1663 次 protected String entityName(Class entityClass) { String entityName = entityClass....2013-08-31 15:50匿名 | 浏览 1663 次protected String entityName(Class<T> entityClass) { String entityName = entityClass.getSimpleName(); Entity entity = entityClass.getAnnotation(Entity.class); if(entity.name()!=null&&!"".equals(entity.name())) { entityName = entity.name(); } return entityName; } 这里大概说是什么意思??
2013-09-06 00:58 提问者采纳该方法用于取得指定的实体类的实体名称。如果指定的实体类的实体标签定义了名称,则取该名称,否则取类名。
例如:有实体类SampleEntity
123456import
javax.persistence.Entity;
@Entity
(name =
"SpecialName"
)
public
class
SampleEntity {
}
以SampleEntity类作为参数调用entityName方法:
1、方法中第一行,String entityName = entityClass.getSimpleName();
变量entityName的值设为类SampleEntity的短名称,即“SampleEntity”。
2、方法中第二行,Entity entity = entityClass.getAnnotation(Entity.class);
即取得SampleEntity的@Entity标签。
3、方法中第三行到第六行,if(entity.name()!=null&&!"".equals(entity.name()))
{
entityName = entity.name();}
即@Entity标签中定义了name并且name不是空串时, 变量entityName的值设为@Entity标签中name的值,即“SpecialName”。
4、返回变量entityName,值为“SpecialName”。
如果SampleEntity的@Entity标签没有定义name
123456import
javax.persistence.Entity;
@Entity
public
class
SampleEntity {
}
方法中第三行,if(entity.name()!=null&&!"".equals(entity.name()))条件不成立,最终方法返回变量entityName,值为“SampleEntity”。
-
使用spring ResponseEntity处理http响应
2018-07-21 11:31:26使用spring ResponseEntity处理http响应 简介 ...本文我们学习如何通过ResponseEntity...ResponseEntity ResponseEntity标识整个http相应:状态码、头部信息以及相应体内容。因此我们可以使用其对http响应实现完...使用spring ResponseEntity处理http响应
简介
使用spring时,达到同一目的通常有很多方法,对处理http响应也是一样。本文我们学习如何通过ResponseEntity设置http相应内容、状态以及头信息。
ResponseEntity
ResponseEntity标识整个http相应:状态码、头部信息以及相应体内容。因此我们可以使用其对http响应实现完整配置。
如果需要使用ResponseEntity,必须在请求点返回,通常在spring rest中实现。ResponseEntity是通用类型,因此可以使用任意类型作为响应体:
@GetMapping("/hello") ResponseEntity<String> hello() { return new ResponseEntity<>("Hello World!", HttpStatus.OK); }
可以通过编程方式指明响应状态,所以根据不同场景返回不同状态:
@GetMapping("/age") ResponseEntity<String> age( @RequestParam("yearOfBirth") int yearOfBirth) { if (isInFuture(yearOfBirth)) { return new ResponseEntity<>( "Year of birth cannot be in the future", HttpStatus.BAD_REQUEST); } return new ResponseEntity<>( "Your age is " + calculateAge(yearOfBirth), HttpStatus.OK); }
另外,还可以设置http响应头:
@GetMapping("/customHeader") ResponseEntity<String> customHeader() { HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "foo"); return new ResponseEntity<>( "Custom header set", headers, HttpStatus.OK); }
而且, ResponseEntity提供了两个内嵌的构建器接口: HeadersBuilder 和其子接口 BodyBuilder。因此我们能通过ResponseEntity的静态方法直接访问。
最简单的情况是相应包括一个主体及http 200响应码:
@GetMapping("/hello") ResponseEntity<String> hello() { return ResponseEntity.ok("Hello World!"); }
大多数常用的http 响应码,可以通过下面static方法:
BodyBuilder accepted(); BodyBuilder badRequest(); BodyBuilder created(java.net.URI location); HeadersBuilder<?> noContent(); HeadersBuilder<?> notFound(); BodyBuilder ok();
另外,可以能使用BodyBuilder status(HttpStatus status)和BodyBuilder status(int status) 方法设置http状态。使用ResponseEntity BodyBuilder.body(T body)设置http响应体:
@GetMapping("/age") ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) { if (isInFuture(yearOfBirth)) { return ResponseEntity.badRequest() .body("Year of birth cannot be in the future"); } return ResponseEntity.status(HttpStatus.OK) .body("Your age is " + calculateAge(yearOfBirth)); }
也可以自定义头信息:
@GetMapping("/customHeader") ResponseEntity<String> customHeader() { return ResponseEntity.ok() .header("Custom-Header", "foo") .body("Custom header set"); }
因为BodyBuilder.body()返回ResponseEntity 而不是 BodyBuilder,需要最后调用。注意使用HeaderBuilder 不能设置任何响应体属性。
尽管ResponseEntity非常强大,但不应该过度使用。在一些简单情况下,还有其他方法能满足我们的需求,使代码更整洁。
替代方法
@ResponseBody
典型spring mvc应用,请求点通常返回html页面。有时我们仅需要实际数据,如使用ajax请求。这时我们能通过@ResponseBody注解标记请求处理方法,审批人能够处理方法结果值作为http响应体。
@ResponseStatus
当请求点成功返回,spring提供http 200(ok)相应。如果请求点抛出异常,spring查找异常处理器,由其返回相应的http状态码。对这些方法增加@ResponseStatus注解,spring会返回自定义http状态码。
直接操作相应
Spring 也允许我们直接 javax.servlet.http.HttpServletResponse 对象;只需要申明其作为方法参数:
@GetMapping("/manual") void manual(HttpServletResponse response) throws IOException { response.setHeader("Custom-Header", "foo"); response.setStatus(200); response.getWriter().println("Hello World!"); }
但需要说明,既然spring已经提供底层实现的抽象和附件功能,我们不建议直接操作response。
总结
本文我们介绍了spring提供多种方式处理http响应,以及各自的优缺点,希望对你有帮助。
-
EntityWrapper
2019-03-26 15:30:33EntityWrapper<Project> project=new EntityWrapper<>(); project.eq("status",4); projectService.selectList(project) Project project=new Project(); project.setStatus(4)...EntityWrapper<Project> project=new EntityWrapper<>(); project.eq("status",4); projectService.selectList(project)
Project project=new Project(); project.setStatus(4); projectService.selectList(new EntityWrapper<>(project));
第二种方法,若project中有属性为int ,则sql 会出现 and 所有int的字段,且默认值为0
有一种猜测:mybatis+ 在拼接sql时,判断字段不为null,则and 上去
而int的默认值是0
/** * 获取所有员工 * @return */ @RequestMapping("/staffAdvisoryList") public List<StaffInfo> staffAdvisoryList(){ EntityWrapper<StaffInfo> entityWrapper = new EntityWrapper<>(); entityWrapper.in("department_id",new Object[]{39,66,68,69}); entityWrapper.eq(CommonConstant.DEL_FLAG,1); List<StaffInfo> staffInfos = staffInfoService.selectList(entityWrapper); return staffInfos; }
-
ResponseEntity返回数据、状态、头部信息
2018-12-12 01:51:30ResponseEntity :标识整个http相应:状态码、头部信息、响应体内容(spring) @ResponseBody:加在请求处理方法上,能够处理方法结果值作为http响应体... ... ResponseEntity事例: import com.baidu.item.po...ResponseEntity :标识整个http相应:状态码、头部信息、响应体内容(spring)
@ResponseBody:加在请求处理方法上,能够处理方法结果值作为http响应体(springmvc)
@ResponseStatus:加在方法上、返回自定义http状态码(spring)
ResponseEntity 事例:
import com.baidu.item.pojo.Category; import com.baidu.item.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @ Author :ssw * @ Date :Created in 10:37 2018/12/11 */ @RestController @RequestMapping("category") public class CategoryController { @Autowired private CategoryService categoryService; @GetMapping("list") public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid",defaultValue = "0") Long pid){ List<Category> categories = this.categoryService.queryByParentId(pid); if (categories != null && 0!=categories.size()) { //返回数据就为http响应体内容 return ResponseEntity.ok(categories); } //返回响应状态码204 return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); }}
公众号 关注一波 不定期分享视频资料
-
Entity Framework 索引
2019-06-17 10:11:54Entity Framwework 6 设置和使用索引,是一个比较 egg 疼的事情,为什么这么说呢?因为Entity Framwework 6的不同版本有不同的设置和使用方法,按照版本来划分,有三种方法: EF6 方法 EF6.1.x方法 EF6.2.x方法 EF... -
xml entity
2015-03-21 06:52:55entity entity翻译为"实体"。它的作用类似word中的"宏",也可以理解为DW中的摸板,你可以预先定义一个entity,然后在一个文档中多次调用,或者在多个文档中调用同一个entity。 entity可以包含字符,文字等等,使用... -
Entity Framework Core 3.1 和 Entity Framework 6.4 发布
2019-12-06 22:56:04目前,Entity Framework Core 3.1和Entity Framework 6.4已正式发布。 EF Core 3.1的获取方式 EF Core 3.1 作为一组 NuGet 软件包专门分发。例如,要将 SQL Server 提供程序添加到您的项目中,可以使用 dotnet ... -
@Entity注解
2018-06-23 14:04:38[@Entity] 必须与@Id注解 结合使用 否则 No identifier specified for entity: name 属性 (可选)实体名称。 缺省为实体类的非限定名称。 该名称用于引用查询中的实体。 该名称不能是Java持久性查询语言... -
422 Unprocessable Entity
2020-04-04 14:23:29这种情况下内存回收token丢失后,访问接口就没有传递token才导致的422 Unprocessable Entity; The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that ... -
cesium entity使用
2020-08-21 12:17:311、查看第一个例子: viewer.entities.add({ position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883), point : { ...可以看到entity通过viewer中的entities加载到场景中,entities是entity的集合 -
entity实体类
2020-11-17 18:23:39Entity实体类: 书写规范: 1.java中包和类的建立和书写: 包名可以自己定义,但最好是全部小写; 包下边的类与接口全部使用驼峰命名规则。 (首字母要大写,第二个单词的首字母也要大写。) 注意:要新建**类而... -
Entity Framework Core 简介
2019-08-27 15:19:17今天来讲解一下 .NET 中的重要成员 Entity Framework Core。Entity Framework Core (以下简称 EF Core),是 EF6 之后微软推出的开源的轻量级可扩展跨平台 ORM 框架。 EF Core 需要和 .NET Core 应用程序一起使用,... -
常用的ResponseEntity.BodyBuilder和自定义ResponseEntity例子
2018-06-20 10:38:43ResponseEntity.ok() 和 ResponseEntity.BodyBuilder() 返回200(HttpStatus.SC_OK)@RequestMapping("/check") public ResponseEntity<String> check() { BodyBuilder builder = ... -
Entity Framework 简介
2018-06-22 10:12:18Entity FrameworkEntity Framework 的全称为 ADO.NET Entity Framework,简称 EF。1、与 ADO.NET 的关系 Entity Framework(实体框架)是微软以 ADO.NET 为基础所发展出来的对象关系对应(O/R Mapping)解决方案,... -
ResponseEntity.BodyBuilder和自定义ResponseEntity例子以及状态码
2019-03-28 17:43:00ResponseEntity.ok() 和 ResponseEntity.BodyBuilder() 返回200(HttpStatus.SC_OK) @RequestMapping("/check") public ResponseEntity<String> check() { BodyBuilder builder = (BodyBuilder) ResponseEntit... -
解决 413 Request Entity Too Large
2018-07-17 17:53:43问题:413 Request Entity Too Large 记录一次413 Request Entity Too Large解决的问题 1.对于有经验的开发着看见这个错误第一反应就是服务器的上传大小设置的不对那么修改服务器上传大小 对于nginx的配置有... -
Entity Framework入门
2017-09-08 15:32:48Entity Framework入门准备工作 如果使用的visual studio 2010,可安装NuGet Package Manager包管理工具然后使用NuGet程序包管理器来安装 EntityFramework。安装之后,引用中就存在 EntityFramework三种不同的... -
解决Request entity too large问题
2019-06-26 11:46:29413 Request Entity Too Large 2.:不带413 Request Entity Too Large 2.解释 两种情况略有不同。 1.是请求文件太大(不包含参数) 2.是请求实体太大(包含参数,文件等) 客户端发送的实体主体部分比服务器... -
No identifier specified for entity 错误
2019-01-23 13:33:48Entity 类中加@Id -
413 Request Entity Too Large
2021-01-05 17:40:51413 Request Entity Too Large Django后台采用uWsgi+Nginx的作为服务器,上传文件的时候报错。原因是请求体的大小超出了Nginx默认的请求体大小。因此,我们只需要在Nginx配置文件添加一条设置请求体最大的大小,... -
什么是Entity Framework
2018-11-02 09:41:20什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架。 Entity Framework让开发者可以像操作领域对象(domain-specific objects)那样操作关系型数据(relational data)。 Entity Framework... -
将“EntityFramework 6.1.3”更新到“EntityFramework 5.0.0”失败。找不到与“EntityFramework 5.0.0”...
2017-03-19 15:16:35PM> Install-Package EntityFramework -Version 5.0.0 正在安装“EntityFramework 5.0.0”。 您正在从 Microsoft 下载 EntityFramework,有关此程序包的许可协议在 ... -
RestTemplate和ResponseEntity
2019-04-30 21:37:56ResponseEntity 简介:继承自HttPEntity类,封装了请求后返回的响应头、响应体和响应状态。 作用:用于controller层向前端返回数据和状态码。 构造器: new ResponseEntity(HttpStatus.OK): http状态码。 new ... -
Entity Framework 和 Sqlite
2017-06-15 13:33:45记录在使用Entity Framework 和 Sqlite遇到的坑 -
ResponseEntity实现文件下载
2017-05-07 22:24:26ResponseEntity需要传入3个参数,分别是:请求体、请求头和状态码 具体的代码如下: @RequestMapping("/testResponseEntity") public ResponseEntity testResponseEntity(HttpSession session) throws IOExce -
XmlEntity
2012-02-01 20:46:34XmlEntity类表示XML数据中的实体声明。该类的主要功能是描述和为XML数据建立实体声明。XmlEntity类继承于XmlNode类,属于XmlNode类的派生类之一。以为在DOM结构中实体声明没有子节点,所以XmlEntity类继承于XmlNode... -
Entity FrameWork 实现分页
2020-05-13 11:11:00SQl语句进行分页 SQL语句进行分页主要是应用Entity FrameWork的SqlQuery()传入SQL语句进行查询... -
EntitySystem
2010-12-28 17:20:00http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/一些优势:data driven----解放coder,更加敏捷快速的开发方式问题在于扩展性,