项目学生:带有Jersey的Web服务服务器

这是Project Student的一部分。 其他职位包括带有Jersey的Webservice Client , 业务层和带有Spring Data的持久性 。

RESTful Web应用程序洋葱的第二层是Web服务服务器。 它应该是一个薄层,用于包装对业务层的调用,但不对其自身进行大量处理。 这篇文章有很多代码,但主要是测试类。

设计决策

泽西岛 -我将泽西岛用于REST服务器。 我考虑了替代方案– Spring MVC , Netty等,但是出于与客户相同的原因,决定选择Jersey。 它轻巧,不会限制开发人员。

依赖注入 –我需要依赖注入,这意味着我需要确定一个框架:Spring,EJB3,Guice等。我已经知道我将在持久层中使用Spring Data ,因此使用它很容易春天的框架。 我仍然会谨慎地最小化对该框架的任何依赖关系(ha!),以实现最大的灵活性。

局限性

球衣 –我不知道球衣将如何处理高负荷。 这是REST服务器必须是业务层的薄包装的关键原因-如果有必要,更改库将相对容易。

用户权限 –没有尝试将对某些方法的访问限制为特定用户或主机。 这应该由业务层来处理,而安全性异常将由REST服务器转换为FORBIDDEN状态代码。

泽西REST服务器

REST API是我们早期的设计文档之一。 对于服务器,这意味着我们从REST服务器开始而不是从业务层API开始实现该层。 实际上,REST服务器在业务层API中定义了必要的方法。

与标准的REST CRUD API有一个小的偏差:对象是使用POST而不是PUT创建的,因为后者的语义是完全按照提供的方式创建了对象。 我们无法做到这一点–出于安全原因,我们从不公开内部ID,也不得接受用户定义的UUID。 这意味着我们将违反REST API合同,因此我们改用POST。

还有一个小作弊:CRUD合同仅需要具有创建或更新对象的能力。 这意味着我们可以仅通过路径就可以找出所需的操作,而无需添加特定的“操作”字段。 随着我们将实现扩展到不仅仅包括CRUD动作,这可能会改变。

继续执行代码...

@Service
@Path("/course")
public class CourseResource extends AbstractResource {private static final Logger log = Logger.getLogger(CourseResource.class);private static final Course[] EMPTY_COURSE_ARRAY = new Course[0];@ContextUriInfo uriInfo;@ContextRequest request;@Resourceprivate CourseService service;/*** Default constructor.*/public CourseResource() {}/*** Unit test constructor.* * @param service*/CourseResource(CourseService service) {this.service = service;}/*** Get all Courses.* * @return*/@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response findAllCourses() {log.debug("CourseResource: findAllCourses()");Response response = null;try {List<Course> courses = service.findAllCourses();List<Course> results = new ArrayList<Course>(courses.size());for (Course course : courses) {results.add(scrubCourse(course));}response = Response.ok(results.toArray(EMPTY_COURSE_ARRAY)).build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Create a Course.* * @param req* @return*/@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response createCourse(Name req) {log.debug("CourseResource: createCourse()");final String name = req.getName();if ((name == null) || name.isEmpty()) {return Response.status(Status.BAD_REQUEST).entity("'name' is required").build();}Response response = null;try {Course course = service.createCourse(name);if (course == null) {response = Response.status(Status.INTERNAL_SERVER_ERROR).build();} else {response = Response.created(URI.create(course.getUuid())).entity(scrubCourse(course)).build();}} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Get a specific Course.* * @param uuid* @return*/@Path("/{courseId}")@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response getCourse(@PathParam("courseId") String id) {log.debug("CourseResource: getCourse()");Response response = null;try {Course course = service.findCourseByUuid(id);response = Response.ok(scrubCourse(course)).build();} catch (ObjectNotFoundException e) {response = Response.status(Status.NOT_FOUND).build();} catch (Exception e) {if (!e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Update a Course.* * FIXME: what about uniqueness violations?* * @param id* @param req* @return*/@Path("/{courseId}")@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response updateCourse(@PathParam("courseId") String id, Name req) {log.debug("CourseResource: updateCourse()");final String name = req.getName();if ((name == null) || name.isEmpty()) {return Response.status(Status.BAD_REQUEST).entity("'name' is required").build();}Response response = null;try {final Course course = service.findCourseByUuid(id);final Course updatedCourse = service.updateCourse(course, name);response = Response.ok(scrubCourse(updatedCourse)).build();} catch (ObjectNotFoundException exception) {response = Response.status(Status.NOT_FOUND).build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Delete a Course.* * @param id* @return*/@Path("/{courseId}")@DELETEpublic Response deleteCourse(@PathParam("courseId") String id) {log.debug("CourseResource: deleteCourse()");Response response = null;try {service.deleteCourse(id);response = Response.noContent().build();} catch (ObjectNotFoundException exception) {response = Response.noContent().build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}
}

该实现告诉我们,我们需要三件事:

  • 服务API(CourseService)
  • 请求参数类(名称)
  • 洗涤器(scrubCourse)

我没有显示完整的日志记录。 必须清除请求参数,以避免日志污染。 。 作为一个简单的示例,请考虑使用写入SQL数据库的记录器,以便于分析。 该记录器的一个简单的实现-不使用位置参数-将允许通过精心设计的请求参数进行SQL注入!

OWASP ESAPI包含可用于日志清理的方法。 我没有包含在这里,因为设置起来有些麻烦。 (应该很快就会在签入代码中。)

为什么要登录到数据库? 一种好的做法是记录到达服务器层的所有未处理的异常-您永远都不想依靠用户来报告问题,而且写入日志文件的错误很容易被忽略。 相反,使用简单工具可以轻松检查写入数据库的报告。

发生未处理的异常时,高级开发人员甚至可以创建新的错误报告。 在这种情况下,至关重要的是维护一个单独的异常数据库,以避免提交重复的条目和使开发人员不知所措。 (数据库可以包含每个异常的详细信息,但错误报告系统每个异常类+堆栈跟踪仅应有一个错误报告。)

服务API

CRUD操作的服务API很简单。

public interface CourseService {List<Course> findAllCourses();Course findCourseById(Integer id);Course findCourseByUuid(String uuid);Course createCourse(String name);Course updateCourse(Course course, String name);void deleteCourse(String uuid);
}

该API还包含一个ObjectNotFoundException。 (这应该扩展为包括无法找到的对象的类型。)

public class ObjectNotFoundException extends RuntimeException {private static final long serialVersionUID = 1L;private final String uuid;public ObjectNotFoundException(String uuid) {super("object not found: [" + uuid + "]");this.uuid = uuid;}public String getUuid() {return uuid;}
}

如上所述,我们最终还需要一个UnauthorizedOperationException。

请求参数

请求参数是封装了POST负载的简单POJO。

@XmlRootElement
public class Name {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

学生和教师也需要电子邮件地址。

@XmlRootElement
public class NameAndEmailAddress {private String name;private String emailAddress;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmailAddress() {return emailAddress;}public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}
}

最终的应用程序将具有大量的请求参数类。

洗涤塔

洗涤塔具有三个目的。 首先,它删除了不应提供给客户端的敏感信息,例如内部数据库标识符。

其次,它可以防止由于引入集合而导致大量的数据库转储。 例如,一个学生应该包括当前部分的列表,但是每个部分都有一个已注册学生和教师的列表。 这些学生和教师中的每一个都有自己的当前部分列表。 进行起泡,冲洗,重复,最终您将转储整个数据库以响应单个查询。

解决方案是仅包含有关每个可以独立查询的对象的浅层信息。 例如,一个学生将拥有当前部分的列表,但是这些部分将仅包含UUID和名称。 一个很好的经验法则是,清理后的集合应完全包含将在下拉列表和表示表中使用的信息,仅此而已。 演示列表可以包括链接(或AJAX操作),以根据需要提取其他信息。

最后,这是执行HTML编码和清理的好地方。 应该清除返回的值,以防止跨站点脚本(CSS)攻击

public abstract class AbstractResource {/*** Scrub 'course' object.** FIXME add HTML scrubbing and encoding for string values!*/	public Course scrubCourse(final Course dirty) {final Course clean = new Course();clean.setUuid(dirty.getUuid());clean.setName(dirty.getName());// clean.setSelf("resource/" + dirty.getUuid());return clean;}
}

配置类

我们有两个配置类。 第一个始终由服务器使用,第二个仅在集成测试期间由服务器使用。 后者的配置(和引用的类)位于集成测试源树中。

我更喜欢使用配置类(在Spring 3.0中引入),因为它们提供了最大的灵活性-例如,我可以根据运行应用程序的用户或环境变量有条件地定义bean-并允许我仍然包括标准配置文件。

@Configuration
@ComponentScan(basePackages = { "com.invariantproperties.sandbox.student.webservice.server.rest" })
@ImportResource({ "classpath:applicationContext-rest.xml" })
// @PropertySource("classpath:application.properties")
public class RestApplicationContext {@Resourceprivate Environment environment;
}

Spring 3.1引入了配置文件。 它们可以工作-但我正在使用的具有弹簧意识的jersey servlet似乎无法正确设置活动概要文件。

@Configuration
//@Profile("test")
public class RestApplicationContextTest {@BeanStudentService studentService() {return new DummyStudentService();}
}

web.xml

现在,我们有足够的资源来实现我们的Web服务器。 所使用的servlet是启用了spring的Jersey servlet,它使用contextClass参数中给出的配置类。 (也可以使用配置文件,但不能同时使用配置类和文件。)

该Servlet还包含spring.profiles.active的定义。 目的是通过spring 3.1 @Profile注释有条件地在RestApplicationContextTest中包含定义,但我无法使其正常工作。 我把它留给以后参考。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Project Student Webservice</display-name><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>com.invariantproperties.sandbox.student.webservice.server.config.RestApplicationContextcom.invariantproperties.sandbox.student.webservice.server.config.RestApplicationContextTest</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>REST dispatcher</servlet-name><servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class><init-param><param-name>spring.profiles.active</param-name><param-value>test</param-value></init-param></servlet><servlet-mapping><servlet-name>REST dispatcher</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping>
</web-app>

单元测试

单元测试很简单。

public class CourseResourceTest {private Course physics = new Course();private Course mechanics = new Course();@Beforepublic void init() {physics.setId(1);physics.setName("physics");physics.setUuid(UUID.randomUUID().toString());mechanics.setId(1);mechanics.setName("mechanics");mechanics.setUuid(UUID.randomUUID().toString());}@Testpublic void testFindAllCourses() {final List<Course> expected = Arrays.asList(physics);final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(200, response.getStatus());final Course[] actual = (Course[]) response.getEntity();assertEquals(expected.size(), actual.length);assertNull(actual[0].getId());assertEquals(expected.get(0).getName(), actual[0].getName());assertEquals(expected.get(0).getUuid(), actual[0].getUuid());}@Testpublic void testFindAllCoursesEmpty() {final List<Course> expected = new ArrayList<>();final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(200, response.getStatus());final Course[] actual = (Course[]) response.getEntity();assertEquals(0, actual.length);}@Testpublic void testFindAllCoursesFailure() {final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(500, response.getStatus());}@Testpublic void testGetCourse() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(expected.getUuid())).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(expected.getUuid());assertEquals(200, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(expected.getName(), actual.getName());assertEquals(expected.getUuid(), actual.getUuid());}@Testpublic void testGetCourseMissing() {final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(physics.getUuid())).thenThrow(new ObjectNotFoundException(physics.getUuid()));final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(physics.getUuid());assertEquals(404, response.getStatus());}@Testpublic void testGetCourseFailure() {final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(physics.getUuid())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(physics.getUuid());assertEquals(500, response.getStatus());}@Testpublic void testCreateCourse() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(201, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(expected.getName(), actual.getName());}@Testpublic void testCreateCourseBlankName() {final Course expected = physics;final Name name = new Name();final CourseService service = Mockito.mock(CourseService.class);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(400, response.getStatus());}/*** Test handling when the course can't be created for some reason. For now* the service layer just returns a null value - it should throw an* appropriate exception.*/@Testpublic void testCreateCourseProblem() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenReturn(null);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testCreateCourseFailure() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testUpdateCourse() {final Course expected = physics;final Name name = new Name();name.setName(mechanics.getName());final Course updated = new Course();updated.setId(expected.getId());updated.setName(mechanics.getName());updated.setUuid(expected.getUuid());final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(expected.getUuid())).thenReturn(expected);when(service.updateCourse(expected, name.getName())).thenReturn(updated);final CourseResource resource = new CourseResource(service);final Response response = resource.updateCourse(expected.getUuid(),name);assertEquals(200, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(mechanics.getName(), actual.getName());assertEquals(expected.getUuid(), actual.getUuid());}/*** Test handling when the course can't be updated for some reason. For now* the service layer just returns a null value - it should throw an* appropriate exception.*/@Testpublic void testUpdateCourseProblem() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.updateCourse(expected, name.getName())).thenReturn(null);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testUpdateCourseFailure() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.updateCourse(expected, name.getName())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testDeleteCourse() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);doNothing().when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(204, response.getStatus());}@Testpublic void testDeleteCourseMissing() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);doThrow(new ObjectNotFoundException(expected.getUuid())).when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(204, response.getStatus());}@Testpublic void testDeleteCourseFailure() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);doThrow(new UnitTestException()).when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(500, response.getStatus());}
}

整合测试

问题: REST服务器集成测试是否应该使用实时数据库?

答:这是一个技巧问题。 我们都需要。

总体架构包含三个Maven模块。 我们之前介绍了student-ws-client,今天介绍了Student-ws-server。 每个都创建一个.jar文件。 第三个模块-student-ws-webapp-创建实际的.war文件。 学生-ws-服务器模块的集成测试应使用虚拟服务层,而学生-ws-webapp模块的集成测试应使用完整堆栈。

我们从集成测试开始,该集成测试反映了客户端模块中的单元测试。

public class CourseRestServerIntegrationTest {CourseRestClient client = new CourseRestClientImpl("http://localhost:8080/rest/course/");@Testpublic void testGetAll() throws IOException {Course[] courses = client.getAllCourses();assertNotNull(courses);}@Test(expected = ObjectNotFoundException.class)public void testUnknownCourse() throws IOException {client.getCourse("missing");}@Testpublic void testLifecycle() throws IOException {final String physicsName = "Physics 201";final Course expected = client.createCourse(physicsName);assertEquals(physicsName, expected.getName());final Course actual1 = client.getCourse(expected.getUuid());assertEquals(physicsName, actual1.getName());final Course[] courses = client.getAllCourses();assertTrue(courses.length > 0);final String mechanicsName = "Newtonian Mechanics 201";final Course actual2 = client.updateCourse(actual1.getUuid(),mechanicsName);assertEquals(mechanicsName, actual2.getName());client.deleteCourse(actual1.getUuid());try {client.getCourse(expected.getUuid());fail("should have thrown exception");} catch (ObjectNotFoundException e) {// do nothing}}
}

我们还需要一个虚拟服务类,该类可以实现足以支持我们的集成测试的功能。

public class DummyCourseService implements CourseService {private Map cache = Collections.synchronizedMap(new HashMap<String, Course>());public List<Course> findAllCourses() {return new ArrayList(cache.values());}public Course findCourseById(Integer id) {throw new ObjectNotFoundException(null);    	}public Course findCourseByUuid(String uuid) {if (!cache.containsKey(uuid)) {throw new ObjectNotFoundException(uuid);    	}return cache.get(uuid);}public Course createCourse(String name) {Course course = new Course();course.setUuid(UUID.randomUUID().toString());course.setName(name);cache.put(course.getUuid(), course);return course;}public Course updateCourse(Course oldCourse, String name) {if (!cache.containsKey(oldCourse.getUuid())) {throw new ObjectNotFoundException(oldCourse.getUuid());    	    		}Course course = cache.get(oldCourse.getUuid());course.setUuid(UUID.randomUUID().toString());course.setName(name);return course;    	}public void deleteCourse(String uuid) {if (cache.containsKey(uuid)) {cache.remove(uuid);}}
}

pom.xml

pom.xml文件应包含一个用于运行嵌入式码头或tomcat服务器的插件。 作为集成测试的一部分,高级用户可以旋转和拆除嵌入式服务器-请参阅更新。

<build><plugins><!-- Run the application using "mvn jetty:run" --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.16</version> <!-- ancient! --><configuration><!-- Log to the console. --><requestLog implementation="org.mortbay.jetty.NCSARequestLog"><!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug that prevents the requestLog from being set. --><append>true</append></requestLog><webAppConfig><contextPath>/</contextPath><extraClasspath>${basedir}/target/test-classes/</extraClasspath></webAppConfig></configuration></plugin></plugins>
</build>

更新资料

经过更多研究后,我进行了配置以在集成测试期间设置和拆除码头服务器。 此配置使用非标准端口,因此我们可以运行它而不必关闭同时运行的另一个码头或tomcat实例。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><build><plugins><!-- Run the application using "mvn jetty:run" --><plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.1.0.v20131115</version><configuration><webApp><extraClasspath>${basedir}/target/test-classes/</extraClasspath></webApp><scanIntervalSeconds>10</scanIntervalSeconds><stopPort>18005</stopPort><stopKey>STOP</stopKey><systemProperties><systemProperty><name>jetty.port</name><value>18080</value></systemProperty></systemProperties></configuration><executions><execution><id>start-jetty</id><phase>pre-integration-test</phase><goals><goal>run</goal></goals><configuration><scanIntervalSeconds>0</scanIntervalSeconds><daemon>true</daemon></configuration></execution><execution><id>stop-jetty</id><phase>post-integration-test</phase><goals><goal>stop</goal></goals></execution></executions></plugin></plugins></build>
</project>

源代码

  • 可从http://code.google.com/p/invariant-properties-blog/source/browse/student/student-webservices/student-ws-server获取源代码。

参考: 项目学生:来自Invariant Properties博客的JCG合作伙伴 Bear Giles提供的带有Jersey的Webservice Server 。

翻译自: https://www.javacodegeeks.com/2014/01/project-student-webservice-server-with-jersey.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/365439.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试

在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试 原文:在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试版权声明&#xff1a;本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布&#xff0c;但务必保留文章署名…

java 通道 双向原理_Java-NIO(四):通道(Channel)的原理与获取

通道(Channel)&#xff1a;由java.nio.channels包定义的&#xff0c;Channel表示IO源与目标打开的连接&#xff0c;Channel类似于传统的“流”&#xff0c;只不过Channel本身不能直接访问数据&#xff0c;Channel只能与Buffer进行交互。通道主要用于传输数据&#xff0c;从缓冲…

HTML标签类型

标签分类&#xff1a; 一、块标签&#xff1a;块标签是指本身属性为display:block;的元素。 1.默认占一行可以设置宽高&#xff0c; 2.在不设置宽度的情况下&#xff0c;块级元素的宽度是它父级元素内容的宽度 3.在不设置高度的情况下&#xff0c;块级元素的高度是它本身内容的…

HashMap的fast-fail和ConcurrentHashMap的fail-safe实例

声明&#xff1a;迁移自本人CSDN博客https://blog.csdn.net/u013365635 Java HashMap遍历过程中如果元素被修改会导致遍历失败&#xff0c;ConcurrentHashMap则不会有这个问题。由此引出HashMap的fast-fail机制和ConcurrentHashMap的的fail-safe机制。 看如下实例。 首先看Hash…

访问权限冲突定义_一文读懂F5 REST API的细粒度角色访问控制

↑ 点击上方“小咩社长”关注我阅读提示&#xff5c;本文大概4718字 阅读需要12分钟写在前面&#xff1a;前两天一个保险的客户联系我说有个需求&#xff0c;问通过调用F5 REST API可否实现&#xff1f;&#xff1a;需要把F5负载均衡上面的配置相关的信息&#xff0c;包含每个…

Java:在JPA中使用规范模式

本文是在Java中使用规范模式的简介。 我们还将看到如何将经典规范与JPA Criteria查询结合使用&#xff0c;以从关系数据库中检索对象。 在本文中&#xff0c;我们将使用以下Poll类作为创建规范的示例实体。 它表示具有开始和结束日期的民意调查。 在这两个日期之间的时间中&am…

python财经数据接口包Tushare pro的入门及简单使用方式(大数据,股票数据接口)...

最近在做一个项目&#xff0c;需要用到股票的数据&#xff0c;我在网上查了很久&#xff0c;最终发现在股票数据上面还是tushare比较专业&#xff0c;而且对于将来做金融行业的大数据这一块的&#xff0c;tushare绝对是你的一个好帮手&#xff0c;所以下面我就简单介绍一下。 一…

java ean13 条形码_【教程】Spire.Barcode 教程:如何在C#中创建EAN-13条码

基于UPC-A标准的EAN-13在世界范围内用于标记零售商品。 13位EAN-13号码由四部分组成&#xff1a;国家代码 - 2或3位数字制造商代码 - 5至7位数字产品代码 - 3至5位数字检查数字 - 最后一位数字代码演示&#xff1a;Step 1: 创建一个BarcodeSettings实例。BarcodeSettings setti…

C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法...

C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法 原文:C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法曾经这个问题困扰我多天最后查资料一大堆&#xff0c;最后终于解决了&#xff0c;看到很多人做C#串…

Module的加载实现

烂笔头开始记录小知识点啦&#xff5e; 浏览器要加载 ES6模块&#xff0c;&#xff1a; <script type"module" src"./foo.js"></script> 异步加载&#xff0c;相当与defer属性。可以另外设置async属性。ES6 模块也允许内嵌在网页中&#xff0…

python 线型_CCF 202006-1 线性分类器 python

题目&#xff1a;线性分类器(line)【题目描述】考虑一个简单的二分类问题——将二维平面上的点分为A和B两类。训练数据包含n个点&#xff0c;其中第i个点(1≤i≤n)可以表示为一个三元组(x,y,type)&#xff0c;即该点的横坐标、纵坐标和类别。在二维平面上&#xff0c;任意一条直…

[Swift]LeetCode682. 棒球比赛 | Baseball Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号&#xff1a;山青咏芝&#xff08;shanqingyongzhi&#xff09;➤博客园地址&#xff1a;山青咏芝&#xff08;https://www.cnblogs.com/strengthen/&#xff09;➤GitHub地址&a…

Java调试器–权威的工具列表

Java调试是一个复杂的空间。 调试器的类型很多&#xff0c;并且有很多工具可供选择。 在此页面中&#xff0c;我们将介绍7种类型的调试器之间的区别&#xff0c;并查看每个类别中的主要工具&#xff0c;以帮助您为正确的工作选择正确的工具。 以下是我们涵盖的调试器类型&…

java项目中多个定时器_在java项目中如何使用Timer定时器

在java项目中如何使用Timer定时器发布时间&#xff1a;2020-11-16 16:36:16来源&#xff1a;亿速云阅读&#xff1a;97作者&#xff1a;Leah在java项目中如何使用Timer定时器&#xff1f;很多新手对此不是很清楚&#xff0c;为了帮助大家解决这个难题&#xff0c;下面小编将为大…

慎使用sql的enum字段类型

在sql的优化中&#xff0c;会有同学提到一点&#xff1a;使用enum字段类型&#xff0c;代替其他tinyint等类型。以前这也是不少人喜欢优化的&#xff0c;但是现在细想&#xff0c;是非常不合理的。 优点&#xff1a; 1.可以设置区间范围&#xff0c;比如设置性别&#xff1a;1男…

js对HTML字符转义与反转义

注意&#xff1a; 在编写html时&#xff0c;经常需要转义&#xff0c;才能正常显示在页面上。 并且&#xff0c;还可以防止xss。 解决方案&#xff1a; 一&#xff0c; 使用正则&#xff1a; 使用正则转码&#xff1a; var value document.getElementById(input).value.t…

python三维数据图_matplotlib中三维数据的热图

我想用我的三维数据生成一张热图。在我已经能够用这些数据绘制出trisurf。在有人能帮我制作热图吗&#xff1f;我看到了在线教程&#xff0c;但是它们对3D来说都很复杂&#xff0c;我在这个网站上找到了一个在matplotlib中生成带有散点的热图&#xff0c;但是它只有2D数据。在我…

区分基于Ant目标的Gradle任务

在我的博客文章《 从Ant Build演变Gradle构建&#xff1a;导入Ant构建文件》中 &#xff0c;我演示了如何使用Gradle内置的基于AntBuilder的Ant支持在Gradle构建中导入Ant目标。 然后&#xff0c;可以将这些Ant目标作为Gradle任务进行访问&#xff0c;并与Gradle构建直接引入的…

java显示长度和第一个字符_从Java字符串中以长度1的字符串返回的第一个字母的最佳方法是什么?...

假设以下内容&#xff1a;String example "something";String firstLetter "";以下分配方式firstLetter可能会影响性能&#xff0c;请注意是否存在差异&#xff1b; 哪个最好&#xff0c;为什么&#xff1f;firstLetter String.valueOf(example.charAt(…

sequelize difference between hasone and hasmany

Query is equal, hasone return the first instance from the collection,hasmany return the whole collection. 转载于:https://www.cnblogs.com/ybleeho/p/9772902.html