var adata = " 1rose";
siye@r480:~/svlution/workspace/springmvc4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springmvc
│ │ ├── controller
│ │ │ └── Manicontroller.java
│ │ ├── domain
│ │ │ └── User.java
│ │ ├── setup
│ │ │ ├── Appconfig.java
│ │ │ └── Webxmlconfig.java
│ │ └── utils
│ │ └── ConstraintsUtils.java
│ ├── resources
│ │ └── source
│ │ └── data.xml
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
└── test
├── java
│ └── ocn
│ └── site
│ └── springmvc
│ └── controller
│ └── Runtest.java
└── resources
├── config
│ └── application.xml
└── source
└── data.xml
22 directories, 12 files
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.22.RELEASEversion>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.22.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>xmlunitgroupId>
<artifactId>xmlunitartifactId>
<version>1.3version>
<scope>testscope>
dependency>
package ocn.site.springmvc.controller;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ocn.site.springmvc.domain.User;
@RestController
public class Manicontroller {
private final Logger logger = Logger.getLogger(this.getClass());
@PostMapping(path = "/test", consumes = MediaType.APPLICATION_XML_VALUE)
public User handler(@RequestBody User user) throws CloneNotSupportedException, JAXBException {
logger.info(user);
InputStream is = this.getClass().getResourceAsStream("/source/data.xml");
if (is == null)
return null;
User value = (User) JAXBContext.newInstance(User.class).createUnmarshaller().unmarshal(is);
return value;
}
}
package ocn.site.springmvc.domain;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@XmlRootElement(name = "book")
public class User implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
@Value("34")
private int id;
@Value("hack")
private String name;
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
@Override
public User clone() throws CloneNotSupportedException {
return (User) super.clone();
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
super();
}
}
package ocn.site.springmvc.setup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import ocn.site.springmvc.utils.ConstraintsUtils;
@Configuration
@EnableWebMvc
@ComponentScan({ "ocn.site.springmvc.controller", "ocn.site.springmvc.domain" })
public class Appconfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public ViewResolver getViewResolver() {
return new InternalResourceViewResolver(ConstraintsUtils.PREFIX, ConstraintsUtils.SUFFIX);
}
}
package ocn.site.springmvc.setup;
import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import ocn.site.springmvc.utils.ConstraintsUtils;
public class Webxmlconfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { Appconfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new CharacterEncodingFilter(ConstraintsUtils.ENCODING, true),
new HiddenHttpMethodFilter() };
}
}
package ocn.site.springmvc.utils;
public interface ConstraintsUtils {
String ENCODING = "UTF-8";
String PREFIX = "/WEB-INF/";
String SUFFIX = ".jsp";
}
<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>springmvc4322display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jspurl-pattern>
<el-ignored>falseel-ignored>
<scripting-invalid>truescripting-invalid>
jsp-property-group>
jsp-config>
web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Insert title here
test
<book>
<id>33id>
<name>hackname>
book>
<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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="ocn.site.springmvc.controller">context:component-scan>
<context:component-scan base-package="ocn.site.springmvc.domain">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
package ocn.site.springmvc.controller;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {
private final Logger logger = Logger.getLogger(this.getClass());
private @Autowired WebApplicationContext wac;
@Test
public void run() throws Exception {
MockMvc build = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/test");
InputStream is = this.getClass().getResourceAsStream("/source/data.xml");
String xmldata = stream2string(is);
logger.info(xmldata);
requestBuilder.content(xmldata);
requestBuilder.contentType(MediaType.APPLICATION_XML);
ResultActions ra = build.perform(requestBuilder);
ra.andExpect(MockMvcResultMatchers.content().xml(xmldata));
ra.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_XML));
ra.andExpect(MockMvcResultMatchers.status().isOk());
}
private String stream2string(InputStream is) throws IOException {
byte[] bytes = new byte[is.available()];
is.read(bytes);
return new String(bytes);
}
}
19-09-03 10:37:24 org.springframework.test.context.web.WebTestContextBootstrapper =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-03 10:37:24 org.springframework.test.context.web.WebTestContextBootstrapper =====>>> Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@402f32ff, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@573f2bb1, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5ae9a829, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6d8a00e3]
19-09-03 10:37:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-03 10:37:25 org.springframework.web.context.support.GenericWebApplicationContext =====>>> Refreshing org.springframework.web.context.support.GenericWebApplicationContext@77cd7a0: startup date [Tue Sep 03 10:37:25 CST 2019]; root of context hierarchy
19-09-03 10:37:25 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping =====>>> Mapped "{[/test],methods=[POST],consumes=[application/xml]}" onto public ocn.site.springmvc.domain.User ocn.site.springmvc.controller.Manicontroller.handler(ocn.site.springmvc.domain.User) throws java.lang.CloneNotSupportedException,javax.xml.bind.JAXBException
19-09-03 10:37:25 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@77cd7a0: startup date [Tue Sep 03 10:37:25 CST 2019]; root of context hierarchy
19-09-03 10:37:25 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@77cd7a0: startup date [Tue Sep 03 10:37:25 CST 2019]; root of context hierarchy
19-09-03 10:37:25 org.springframework.mock.web.MockServletContext =====>>> Initializing Spring FrameworkServlet ''
19-09-03 10:37:25 org.springframework.test.web.servlet.TestDispatcherServlet =====>>> FrameworkServlet '': initialization started
19-09-03 10:37:25 org.springframework.test.web.servlet.TestDispatcherServlet =====>>> FrameworkServlet '': initialization completed in 19 ms
19-09-03 10:37:25 ocn.site.springmvc.controller.Runtest =====>>>
33
hack
19-09-03 10:37:26 ocn.site.springmvc.controller.Manicontroller =====>>> User [id=33, name=hack]
19-09-03 10:37:26 org.springframework.web.context.support.GenericWebApplicationContext =====>>> Closing org.springframework.web.context.support.GenericWebApplicationContext@77cd7a0: startup date [Tue Sep 03 10:37:25 CST 2019]; root of context hierarchy
If WebMvcConfigurer does not expose some advanced setting that needs to be configured,
consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport or DelegatingWebMvcConfiguration, e.g.:
private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder",
WebMvcConfigurationSupport.class.getClassLoader());
private static final boolean jackson2XmlPresent =
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper",
WebMvcConfigurationSupport.class.getClassLoader());
if (jackson2XmlPresent) {
messageConverters.add(new MappingJackson2XmlHttpMessageConverter(
Jackson2ObjectMapperBuilder.xml().applicationContext(this.applicationContext).build()));
}
else if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}