Spring 3.2 Application stopped serving JSON, now sending 406 error - spring-mvc

Ok, I am working on a Spring 3.2 project that was serving RESTful JSON.
I have this web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<servlet>
<servlet-name>saltCityWifi</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>saltCityWifi</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name>Salt City Wifi</display-name>
</web-app>
and this servlet-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.saltcitywifi" />
<mvc:annotation-driven />
</beans>
Here is my controller:
package com.saltcitywifi.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.saltcitywifi.model.HotSpot;
import com.saltcitywifi.service.HotSpotService;
import com.saltcitywifi.service.HotSpotServiceImpl;
#Controller
public class HotSpotController {
#RequestMapping(value="/hotSpot", method = RequestMethod.GET)
public #ResponseBody List<HotSpot> getHotSpots() {
HotSpotService hotSpotService = new HotSpotServiceImpl();
List<HotSpot> spots = hotSpotService.getAllHotSpots();
return spots;
}
}
Everything was working fine on Friday, came back to do a little bit of learning on this project and am suddenly getting a 406 error "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
I think that the strongest possibility is that for some reason the Jackson asl dependency is no longer being included on my build path -- but I see the jar inside of the maven dependency folder when I click configure build path.
Any help with this is greatly appreciated (and I have tried clean / update project and read tons of these similar issues deal with here to no avail.).

UGH. Finally figured it out after I posted the question. I was navigating to localhost:8080/myapp/my-route.html.
Navigating to localhost:8080/myapp/my-route.json OR localhost:8080/myapp/my-route works correctly as expected.
The 406 error wasn't a problem with my application, it was a problem with my request -- I don't have any html responses available, but I am accepting the requests for .html in my web.xml file when I declare the url pattern /*.
Hopefully someone sees this and avoids hours of tracking down potential dependency and build path issues and just learns to make the proper request.

Related

Rest API for creating new workspace in Windchill

I want to create a workspace in Windchill calling Rest API from our web application. But no where to find such API end point in any of the Windchill Rest API documentation.
Is it possible to create workspace using rest API, if not is there any alternative way to achieve it.
On which Windchill version do you work?
When I don't find standard endpoints in the REST Services, I write them myself.
Since Windchill 11.1 I use Spring to create them.
For that you need an entry in codebase/WEB-INF/web.xml like:
<servlet>
<servlet-name>ConnectorName</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConnectorName</servlet-name>
<url-pattern>/servlet/connector/*</url-pattern>
</servlet-mapping>
In the same folder you need a file named like the Servletname-servlet like ConnectorName-servlet.xml with following content:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
<context:component-scan base-package="packagename" />
<mvc:annotation-driven />
After this you can create a Spring RestController Class in the package defined above in component-scan.

spring mvc unable to map url

My dispatcher servlet is as follows
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.springimplant.mvc.controllers"/>
</beans>
I have a home controller under the namespace Home controller as follows
package com.springimplant.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class HomeController {
#RequestMapping("/home")
#ResponseBody
public String goHome()
{
return "Welcome Home";
}
}
Web.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>course-project</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListner</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Still when I run the application in localhost I am getting a 404 error as
"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
Kindly Guide where I am going wrong
The url I am hitting is
http://localhost:8080/course-project/home
Please remove #ResponseBody, as dispatcher servlet should understand that the returning value is not a HTTP Response rather it is a view name. And also please make sure that the returning value should be same as your view page name.
Actually I was trying to post that string as ResponseBody only.
Here is what worked for me I started with a fresh project with following changes
Downloaded STS(Spring Tool Suite) again(my console debugging log was not showing up).
Used Tomcat 8.5 instead of pivotal TC server 4.0 you can try pivotal TC 3.2-3.5 which is based on same.
Dynamic Web Module Facet I was using 4.0 I degraded it to 3.1.(I saw somewhere that 4.0 supports #RestController annotation rather than #Controller annotation)
I was using slf4j-log4j13 facet and log4j implementation this was giving an error sth like class not found "log.logger".So I added another facet "log4j-simple".
Though I still have clue what out of these worked for me :)
Thanks for your responses.
Here it is on github
https://github.com/gauravmatta/springmvc/tree/master/my-project

Servlet Programming Using Eclipse IDE

I have started learning servlet programming. I am using eclipse IDE. I have created a simple dynamic web project and have one html file and one servlet program in it(LoginForm.html & LoginServlet.java). I want to display the html page to the user at first .I have also set up the welcome-file-list tag in the web.xml but when I run the project it doesn't display that page. I also tried to enter the url in the browser manually but it's also not working.It is giving the error as '404-Page not found'. My web.xml file is
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LoginApp</display-name>
<welcome-file-list>
<welcome-file>LoginForm.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.myprograms.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/logapp</url-pattern>
</servlet-mapping>
</web-app>
The URL I am using is http://localhost:8001/LoginApp/LoginForm.html. Please suggest any solution.

JSP Site web.xml issue Page not found for url-pattern /MyController in sevlet-mapping

<?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">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MyController</servlet-name>
<servlet-class>com.pk.MyController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyController</servlet-name>
<url-pattern>/MyController</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I have this configuration, but unable to access hzhfyp.com/MyController (PAGE NOT FOUND)
The Path for MyController servelet is WEB-INF/classes/com/pk/MyController.class
Although index.jsp is loaded accuratelty. Demo here http://hzhfyp.com/ Clicking any button will generate js error visible in Firebug(firefox) as Page not Found.
URLs are case sensitive. You've mapped it on /MyController with M, but your jQuery code is calling it by /myController with m. Fix it accordingly.
As to the servlet returing a 404 in spite of the correct URL, this can happen when the servlet failed to initialize or when you didn't deploy the correct web.xml at all. Read the server startup logs for any errors during servlet initialization and verify if you're deploying with the right web.xml.

Spring MVC and Flex integration over BlazeDS?

Which is the best way to integrate existent spring-MVC-Project with flex. I'am using Spring-2.5 lib with annotations.
e.g my list controller:
package xxx.xxx.controller;
#Controller
public class ListController {
#Autowired
private ColorHome colorHome;
#RequestMapping("/admin/colors.do")
public ModelMap colorsHandler() {
Collection<Object> colors = this.colorHome
.findColors();
return new ModelMap(colors);
}
I have also a colors.jsp which displays the colors. Now I would like to integrate flex as an UI-Layer. I only need to integrate the Spring-View with shown RequestMappings above.
Go get BlazeDS. Install it using the WAR file.
You'll also need the flex jar from Spring.
In your web.xml file, add this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/flexContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
Create a flex-servlet.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</beans>
Create a flexContext.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
...
">
<flex:message-broker />
<flex:remoting-destination destination-id="flexService" ref="beanFromApplicationContext" />
</beans>
This should be enough to get you the remoting endpoints.
In Flex, create a remoteObject and give it a destination of "flexService" or whatever you set the destination-id on the to.
Spring has a project for its integration with Flex,BlazeDS and Java.
This might help you. Spring n Flex integration

Resources