Tag: spring mvc

如何在tomcat服务器上部署spring boot web应用程序

我已经创建了spring boot web应用程序,但我无法在tomcat上部署spring boot web应用程序WAR文件,我可以将其作为java应用程序运行。 如何在tomcat上将spring boot应用程序作为Web服务运行。 我正在使用以下代码。 如果可以在tomcat上运行,请在不使用web.xml和使用web.xml的情况下帮助我使用注释。 @SpringBootApplication public class Application extends SpringBootServletInitializer { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } 以下代码为rest控制器 @RestController public class HelloWorld{ @RequestMapping(value = “/hello”, method = RequestMethod.GET) public ResponseEntity get() { return new ResponseEntity(“Hello World”, […]

不直接编写Servlet以创建REST API的原因

在我目前的公司中,我们正在开始一个新项目,该项目将是Java中的REST API,部署在像Tomcat这样的servlet容器中。 在我之前使用REST框架(如JAX-RS和Jersey,JBOSS REST Easy,Spring MVC)的经验中,我知道使用类似于直接编写Servlet来处理请求的框架的一些优点。 (当然我们知道所提到的框架仍然使用Servlets) 我发现很难说服他们。 因为他们建议编写servlet,认为它对性能更好(可能是这种情况,但我认为使用其中一个框架的开销对REST API来说应该是微不足道的)。 以下是我的理由: 1) 更少的样板和更简洁的代码 (更易于维护和测试)。 使用JAX-RS框架或SpringMVC,您可以通过编写带有注释的方法来非常轻松地定义REST资源,注释指示资源的PATH,要使用的http方法,查询和url参数,接受的编码等标题等。 例: @GET @Path(“/users”) @Produces({MediaType.APPLICATION_JSON}) public UserList getUsers(@QueryParam(“group”) String group) { return userService.findUsers(group); } 使用servlet,您至少需要这样的东西: 在web.xml中映射每个servlet的url(在Servlet 3.0中不需要): UsersServlet test.UsersServlet UsersServlet /users 然后在servlet类中: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String group = request.getParameter(“group”); response.setContentType(“application/json”); PrintWriter out = response.getWriter(); […]

在Spring Servlet项目的web.xml中加载contextConfigLocation的顺序

假设我有一个Spring Java项目,我正在尝试将其配置为Web服务器servlet。 这是web.xml文件的精简版本: contextConfigLocation /WEB-INF/spring/generalApplicationContext.xml my-servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/specificApplicationContext.xml 1 my-servlet /foo/* 这里需要注意的关键是我已经指定了两个要加载的XML文件。 一个是我的整个应用程序的通用,而另一个是特定于“my-servlet”servlet。 对于只有一个servlet映射的设置,这没有意义。 但是,我的项目有多个servlet映射,每个都有特定的Spring设置。 我的问题: Spring将首先加载哪个contextConfigLocation? 它是generalApplicationContext.xml还是specialApplicationContext.xml? 更重要的是,装载的顺序是否重要? 从我的调试工作来看,它似乎很明显,因为当我将一些独立的Spring配置从一个文件移动到另一个文件时,我得到了不同的错误。 注意:对于多个servlet映射是否使用多个弹簧配置是一个好的做法是值得商榷的。 使用XML配置而不是新的Java配置也是如此。 但这不是我在这里要问的问题。 让我们试着关注我的主要问题。

如何使用Spring Security / Spring MVC处理表单登录

简单的问题,我只需要一个指向正确方向的指针: 我有一个简单的Spring MVC / Spring Security webapp。 最初我设置了Spring Security,以便默认登录页面正确显示和validation(我使用DaoAuthenticationProvider实现了UserDetailsService来执行此操作)。 下一步:使用我的登录页面替换默认的spring登录页面并发布凭据。 但是我如何处理提交的登录凭据? 我假设我将表单发布到控制器,validation凭据,但我不清楚在此之后正确的步骤是什么。 例如: 我在调用AuthenticationManager的方法吗? 我需要为此定义一个bean吗? 我需要像AuthenticationEntryPoint那样实现一个接口/服务吗? 我已经完成了3次文档,并没有完全遵循它们。 我知道这很简单,所以我只需要听听过程应该如何流动。

删除Transfer-Encoding:在POST请求中分块?

我使用以下代码发送POST请求,但请求以chunked( Transfer-Encoding: chunked )的forms发送。 我搜索了这个问题并说它包含了Content-Length但是在下面的代码中我无法弄清楚如何设置Content-Length : @RequestMapping(value = “/contacts”, method = RequestMethod.POST) public Map addContactInfo( @RequestBody Map ContactInfoDto) { ContactInfo contactInfo = ContactInfoDto.get(“contact”); if (contactInfo == null) { throw new IllegalArgumentException(“Contact not found.”); } contactInfo = this.contactInfoManager.addNew(contactInfo); Map map = new HashMap(); map.put(“contact”, contactInfo); return map; }

Spring @Scheduled任务运行两次

我正在创建一个@Scheduled任务,每5秒运行一次。 由于其他问题一直存在问题,我的任务是运行两次! 我已经查看了其他问题,并在此阅读了适用的文档,但我无法弄清楚问题所在。 我知道当我启动tomcat服务器时,我的@Scheduled类的两个单独实例正在实例化。 我还想到了它们在引用我的日志文件时被实例化的时候。 与此日志行相关联的一个: 信息:初始化Spring根WebApplicationContext 另一个有这个日志行: 信息:初始化Spring FrameworkServlet’servlet’ 这是spring配置文件。 而我的简单java类: package scheduled; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class Notifier { @Scheduled(fixedDelay = 5000) public void notifyUsersOfBidItems() { try { System.out.println(this); } catch (Exception e) { e.printStackTrace(); } } } 另外,我使用的是Spring 4。 编辑: Adding web.xml Archetype Created Web Application servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring_config.xml […]

如何自定义Spring Data REST以使用存储库资源的多段路径?

我正在使用Spring Data JPA和Spring Data REST开发基于组件的CRUD应用程序。 我有几个组件。 例如, 系统组件具有User模型和UserRepository 。 组件由包名称区分。 例如com.example.app. 因此,为了使我的REST API看起来更干净,我需要实现API URL,如下所示。 host:8080// 例如 host:8080/system/users 我在我的存储库中执行了以下操作 @RepositoryRestResource(collectionResourceRel = “users”, path = “system/users”) public interface UserRepository extends PagingAndSortingRepository { … } 当我转到http://localhost:8080时,会生成以下内容 { “_links”: { “users”: { “href”: “http://localhost:8080/system/users{?page,size,sort}”, “templated”: true }, … 但是当我转到http://localhost:8080/system/users 它给出了一个错误 5月22日星期五17:56:37 IST 2015出现意外错误(type = Not Found,status = 404)。 没有消息可用 […]

我无法访问Spring-MVC中的Robots.txt

我试图在Spring-MVC中访问robots.txt 。 为了测试代码,我将robots.txt放在WebContent , Root和WEB-INF但是我无法访问它们中的任何一个。 我已经应用了这些问题的答案1,2,3无济于事。 mycode的

使用Spring MultipartFile和谷歌应用引擎上传文件

我一直在尝试使用MVC和Google App引擎上传文件。 每次我收到错误之类的 预期的MultipartHttpServletRequest:是否配置了MultipartResolver? 之后我提到了两个教程来上传文件。 每次流程转到控制器,但能够访问使用Spring MVC和Google App引擎在jsp文件中上传的MultipartFile文件。 这两个参考是 http://alasdoo.com/2010/10/how-to-upload-a-file-with-spring-mvc-3-and-google-app-engine/ https://code.google.com/p/gmultipart/ 所以任何人都可以指导我解决问题的参考文献中的错误。

什么是在spring启动应用程序中安排任务的最佳方法

我目前正在开发基于Spring-Boot的应用程序。 我知道像@Scheduled这样的注释可以安排任务。 由于我的应用程序中的用户想要在不同的时间发送邮件并且只发送一次。 我已经阅读过Spring调度任务 – 只运行一次 ,但在基于Spring的应用程序中总是“新”一个localExecutor很奇怪。 这样,一旦用户安排发送电子邮件,我就必须为他的任务“新”一个localExecutor。 那么,还有更好的方法吗?