Java SpringWebService返回并附加到url
我有一个简单的web服务,如下所示:Java SpringWebService返回并附加到url,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我有一个简单的web服务,如下所示: @RequestMapping(value="/api") @Controller public class TestController { @RequestMapping(method = RequestMethod.GET) public String hello() { System.out.println("this test is done "); return "this is test url"
@RequestMapping(value="/api")
@Controller
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public String hello() {
System.out.println("this test is done ");
return "this is test url";
}
}
我的web.xml文件如下所示
<servlet-name>pa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pa</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
HTTP Status 404 - /pa/this is test url
type Status report
message /pa/this is test url
description The requested resource (/pa/this is test url) is not available.
Apache Tomcat/7.0.27
返回字符串被附加到url
这是我的spring_context.xml
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:logger/log4j.properties</value>
<!-- log4j.refresh.interval -->
<value>5000</value>
</list>
</property>
</bean>
类路径:logger/log4j.properties
5000
不仅仅是答案,你能告诉我容器里发生了什么,为什么会发生。
如果需要什么,请告诉我。
Thanx.在支持的方法返回类型部分解释了这一点
以下是受支持的返回类型:
[……]
- 解释为逻辑视图名称的字符串值,带有
通过命令对象和
@ModelAttribute注释的引用数据访问器方法李>
您的方法返回一个字符串
,因此其值被解释为视图名称。您可能在上下文中声明了一个InternalResourceViewResolver
,它使用视图名称并尝试解析InternalResourceView
。这是通过使用带有视图名称的HttpServletRequest#getRequestDispatcher()
来完成的(前缀和后缀按配置)
阅读文档我知道它发生的原因。。。
事情是这样的:
问题是
All controllers in the Spring Web MVC framework return a ModelAndView instance.
Views in Spring are addressed by a view name and are resolved by a view resolver.
所以,返回的内容将充当视图,并因此附加到URL
为了解决这个问题,我使用@ResponceBody
,如下所示
@RequestMapping(value="/api")
@Controller
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public @ResponceBody String hello() {
System.out.println("this test is done ");
return "this is test url";
}
}
现在@ResponceBody做什么:
它表明HTTP消息转换器机制应该接管并将返回的对象转换为客户机需要的任何形式。这将告诉spring查看HTTP接受头并选择适当的转换器。如果我们提供带有application/json值的Accept头,那么将调用json MappingJacksonHttpMessageConverter转换器
如果需要进一步的信息,请告诉我。因此我没有使用任何视图解析器,我也会为其输入代码。@MayurGupta输入您的完整上下文。您对我给您的答案不满意吗?您的第一个报价不适用于您的情况。您既没有返回模型,也没有返回视图该规则不适用于Spring 3+。@SotiriosDelimanolis。。。你告诉我容器的工作原理,为什么它会在返回值后面附加参数,但你没有告诉我解决方法。但我真的很感激你的回答。。。谢谢。什么的解决方案?你的问题没有说明如何实现不同的行为。