简体   繁体   English

从Java调用spring servlet

[英]Call a spring servlet from Java

I'm new with spring MVC and I'm trying to access a spring servlet from my Java application and print the result. 我是Spring MVC的新手,我正尝试从Java应用程序访问spring servlet并打印结果。 But I can't make it working. 但是我无法使其正常工作。 I can access a index.jsp file from the navigator at url : http://localhost:8080/MyApp/ But my main class raise me an exception java.io.FileNotFoundException: http://localhost:8080/MyApp/helloWorld . 我可以从导航器的url处访问index.jsp文件: http:// localhost:8080 / MyApp /但是我的主类向我提出了一个异常java.io.FileNotFoundException: http:// localhost:8080 / MyApp / helloWorld Can anyone help me ? 谁能帮我 ?

Here is my web.xml: 这是我的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>MyApp</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <!-- will look for application-servlet.xml file to load -->
        <servlet-name>application</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>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

My application-servlet.xml 我的应用程序servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.myapp.server.controllers" />
    <context:component-scan base-package="com.myapp.server.managers" />
    <context:component-scan base-package="com.myapp.server.repositories" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

My spring controller 我的弹簧控制器

@Controller
public class ApplicationController {

    @Autowired
    private ApplicationManager applicationManager;

     @RequestMapping("/helloWorld.do")
     public String helloWorld() {
        return "helloWorld";
     }

}

My Java code 我的Java代码

public class Test {

    public static void main(String[] args) throws IOException {
        String url = "http://localhost:8080/MyApp/helloWorld";

        URLConnection connection = new URL(url).openConnection();
        connection.setRequestProperty("Accept-Charset", charset);
        InputStream response = connection.getInputStream();

        System.out.println(response.read());
    }

}

It seems that you are trying to hit a non existing endpoint. 看来您正在尝试击中不存在的端点。 You map the @RequestMapping("/helloWorld.do") and you are trying to call the /helloWorld 您映射@RequestMapping(“ / helloWorld.do”)并尝试调用/ helloWorld

Try to rename your request mapping to @RequestMapping("/helloWorld") 尝试将您的请求映射重命名为@RequestMapping(“ / helloWorld”)

If you want to use .do try to add 如果要使用.do,请尝试添加

<url-pattern>*.do</url-pattern>

to your servlet mapping 到您的servlet映射

Here you can find a spring mvc show case 在这里您可以找到Spring MVC展示柜

https://github.com/spring-projects/spring-mvc-showcase https://github.com/spring-projects/spring-mvc-showcase

Hope it helps. 希望能帮助到你。

If you want to maintain you .do extension then replace your servlet mapping as follows. 如果要维护.do扩展名,请按以下步骤替换servlet映射。

<servlet-mapping>
        <servlet-name>application</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

Also remove the .do extension in @RequestMapping("/helloWorld.do") to be @RequestMapping("/helloWorld").By default Spring matches /helloWorld plus any extension ie /helloWorld.do , /helloWorld.htm etc 还要将@RequestMapping(“ / helloWorld.do”)中的.do扩展名删除为@RequestMapping(“ / helloWorld”)。默认情况下,Spring匹配/ helloWorld以及任何扩展名,例如/helloWorld.do,/helloWorld.htm等

This provides flexibility in that you only need to change your servlet mapping in web.xml to use a different extension 这提供了灵活性,因为您只需要更改web.xml中的servlet映射即可使用其他扩展名。

In your test class use the url http://localhost:8080/MyApp/helloWorld.do 在测试类中,使用URL http:// localhost:8080 / MyApp / helloWorld.do

Thank you all for answering. 谢谢大家的回答。 Reading your post and some documentation on googlefriend, I finally solved my issue. 通过阅读您的帖子和有关googlefriend的一些文档,我终于解决了我的问题。 Of course, this needs to be done in my case : 当然,在我的情况下需要这样做:

<servlet-mapping>
        <servlet-name>application</servlet-name>
        <url-pattern>*.do</url-pattern>
</servlet-mapping>

And then in the spring controller : 然后在spring控制器中:

     @RequestMapping("/helloWorld.do")
     public @ResponseBody String connexionApplication() {
        return "Hello World !";
     }

So finally if I call " http://localhost:8080/MyApp/helloWorld " from my browser or from my java code it returns me Hello World ! 因此,最后,如果我从浏览器或Java代码中调用“ http:// localhost:8080 / MyApp / helloWorld ”,它将返回我Hello World!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM