I am considerably new to Spring. What I know is that Spring boot has an embedded tomcat server which can make my Web Service run on a defined host:port.
I prefer to use an independent configurable JBoss instead and make a WS.
I am not sure about the structure of that project, neither do I have a bit of idea what type of context to be declared where!
This is the project structure that I have created. Not sure if this is correct.
JBoss runs fine but on each request, I am getting 404
Here is the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>AdobeSystems</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
And here is my controller code:
#RestController
public class CalculateControllerImpl implements CalculateController{
private CalculateService calculateService;
private DataObjectSRO dataObjectSRO;
public CalculateControllerImpl(){
calculateService = new CalculateServiceImpl();
}
#RequestMapping("/add")
#ResponseStatus(HttpStatus.OK)
public #ResponseBody int add(#RequestParam(value="first", defaultValue="10") int first,#RequestParam(value="second", defaultValue="5") int second ) {
dataObjectSRO.setFirst(first);
dataObjectSRO.setSecond(second);
return calculateService.add(dataObjectSRO.getFirst(),dataObjectSRO.getSecond());
}
}
Related
I'm using Spring Boot on weblogic 10.3.6; however we usually use the embedded tomcat during development.
I'd like to enable a specific Spring profile when on weblogic to accomodate for different configuration/beans to be used when on wl. Since I'm forced to use a web.xml on that ancient version of the application server, I'd like to use web.xml to activate the weblogic profile I need.
This is my web.xml
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.class.annotated.with.SpringBootApplication</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
So far I've seen suggestion to use
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</context-param>
or
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</init-param>
in the DispatcherServlet configuration.
I even tried configuring an ApplicationContextInitializer, but SpringBootContextLoaderListener provides his own initializer.
Is there a proper way to accomplish what I want? I can't put weblogic configuration in main application.yml because that would break the embedded tomcat configuration we are using for development.
Im new to spring with crystal reports I have created my application where I used some java annotation configuration. Now I am trying to integrate with crystal reports where I have to convert xml based web.xml to java based config
here is the web.xml code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- crystal report in spring -->
<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/crystalreportviewers</param-value>
</context-param>
<context-param>
<param-name>crystal_image_use_relative</param-name>
<param-value>webapp</param-value>
</context-param>
<servlet>
<servlet-name>CrystalReportViewerHandler</servlet-name>
<servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CrystalReportViewerHandler</servlet-name>
<url-pattern>/CrystalReportViewerHandler</url-pattern>
<url-pattern>/faces/CrystalReportViewerHandler</url-pattern>
</servlet-mapping>
</web-app>
I have tried to mix it with java config but it fails,
Thanks in advance
To convert your basic web.xml to java configuration, either create a class by
implement WebApplicationInitializer or
extend AbstractAnnotationConfigDispatcherServletInitializer
regarding your crystal report you can create the Bean of type CrystalReportViewerServlet using #Bean Annotation.
for more information please check
- web.xml to java config
and register secondary servlet in spring
Thanks for to all who looked for a solution. I did the following
#Configuration
#ComponentScan(basePackages = { "t.g.app" })
public class ReportsConfig implements WebApplicationInitializer{
#Override
public void onStartup(ServletContext servletContext) throws ServletException{
servletContext.setInitParameter("crystal_image_uri","/crystalreportviewers");
servletContext.setInitParameter("crystal_image_use_relative", "webapp");
ServletRegistration.Dynamic crystalReportViewerHandler = servletContext
.addServlet("CrystalReportViewerHandler", new CrystalReportViewerServlet());
crystalReportViewerHandler.setLoadOnStartup(1);
crystalReportViewerHandler.addMapping("/CrystalReportViewerHandler");
}
}
There were also some security issues caused by spring security so i added some exceptions in security configuration.
To describe my question, I will demonstrate the purpose of my Maven Web Application. I am developing a booking service which allows people to book taxi. The booking services work well but my problem is the booking request from yesterday and previous days must be canceled by a background task (It may have another name but I do not know how to call it correctly, I apologize). This task have a function that scan the database each minute to check the status of the booking, if it expired, cancel it. I defined a class called Main.java and configure my web.xml file like this but there is no result in the system log.
My Main.java
package com.bvps.bacground;
public class Main {
public static void main(String[] args){
System.err.println("123123123123213");
}
}
And the web.xml
<?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>bvps</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>bvps.Main</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>bvps.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
</web-app>
Many thanks for reading this, please help me!
I am creating a webapp with Mule 3 and Spring mvc 3, the problem is I am not able to get mule beans in spring controllers. My application context load with mule context and is not available in spring context, when I load application context in spring context then mule and spring both have different reference to a single bean. How can I get mule context in spring mvc controller so that I can refer to the same bean object. For ref my web.xml is defined below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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"
id="return-label-service" version="2.5">
<display-name>return</display-name>
<context-param>
<param-name>org.mule.config</param-name>
<param-value>mule-return-label-flow.xml,applicationContext.xml </param-value>
</context-param>
<context-param>
<param-name>mule.serverId</param-name>
<param-value>return-label-service</param-value>
</context-param>
<listener>
<listener-class>
org.mule.config.builders.MuleXmlBuilderContextListener
</listener-class>
</listener>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath://applicationContext-web.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
This is a possible duplicate of this question. Moreover, I think there is good documentation available here and here
This issue is solved. Someone created a file called mvc-dispatcher.xml and it had an incorrect configuration. That file is loaded automatically because it's called the same as a servlet.
I want to thank everyone who tried to help me fix this issue. I'm keeping this question here, since it actually explains how to create a REST interface. It works perfectly.
I'm trying to set up a RESTful interface with Spring-MVC. The server starts without issue, but any time I try to call the REST interface I get the message:
41 WARN [springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/myweb/rest/asd/aaa] in DispatcherServlet with name 'mvc-dispatcher'
It seems that the URL I am sending (http://localhost:8080/myweb/rest/asd/qwe for example) is not 'captured' by any controller. What am I doing wrong?
I do not know what else I could try. I'm using Java 1.7.0_15, Tomcat 7.0.34 and Spring 3.1.4.RELEASE
In my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyWeb</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Listener for MVC spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Servlet for MVC spring -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Loading web properties -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
In my applicationContext.xml:
<context:component-scan base-package="com.myweb.*" />
<context:annotation-config/>
<tx:annotation-driven/>
<mvc:annotation-driven />
And finally, my controller class:
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class RestController {
private static Logger LOG = Logger.getLogger(RestController.class);
#RequestMapping("/asd/{test}")
public void test(#PathVariable String test) {
LOG.info("You sent "+test);
}
}
Tried changing the method's #RequestMapping but still didn't work:
#RequestMapping(method=RequestMethod.GET)
public void test() {
LOG.info("You sent something");
}
Your mapping is wrong.
You are requesting /rest/asd/aaa but your mapping is for /asd/{test}.
You need to change #RequestMapping("/asd/{test}") to #RequestMapping("/rest/asd/{test}")
Or add #RequestMapping("/rest") to your controller class
Try annotate your controller class also with #RequestMapping("/rest") to handle all requests with url
/webapp/rest/
You should also configure your context via org.springframework.web.context.ContextLoaderListener listener. I think You are not loading applicationContext.xml
Add this part to your web.xml
<!--spring bootstrap listener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>