我是使用肥皂网服务的春季MVC的新手。
我使用spring-mvc制作了一个Web项目,该项目具有@Controller可以处理网站视图(使用模型)。对于业务逻辑,已使用@Service和@Repository类。
现在,我正在制作具有与网站视图相同的业务逻辑的Web服务。为此,我制作了一个Web服务类,并使用@Autowired现有的@Service进行业务逻辑。但是那个@Autowired类引用了null。
下面是我的示例项目代码,其中
我已经为业务逻辑和tomcat catalina( com.sun.xml.ws.server.sei.TieHandler.createResponse null)设置了服务实现类和自动装配的服务类
java.lang.NullPointerException
在com.springmvcmaven.ws.producer.hello.HelloWSImpl.hello(HelloWSImpl.java:34)
dispatcher-servlet.xml
<context:annotation-config/>
<context:component-scan base-package="com.springmvcmaven" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
pom.xml
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="com.springmvcmaven.ws.producer.hello.HelloWSImpl" name="hello" url-pattern="/hello"/>
</endpoints>
模型类-User.java
public class User
{
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
用于Web的模型控制器-HomeController.java
@Controller
public class HomeController
{
/**
* Simply selects the home view to render by returning its name.
* @param locale
* @param model
* @return
*/
@Autowired
@Qualifier(value = "homeService")
@Lazy(true)
private HomeService service;
@RequestMapping(value = "/home.do", method = RequestMethod.GET)
public String home(Locale locale, Model model)
{
System.out.println("Home Page Requested, locale = " + locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
@RequestMapping(value = "/user.do", method = RequestMethod.POST)
public String user(@Validated User user, Model model)
{
System.out.println("User Page Requested");
model.addAttribute("userName", service.wsCall(user));
return "user";
}
}
HomeService.java
public interface HomeService
{
public String wsCall(User user);
}
HomeServiceImpl.java
@Service(value = "homeService")
public class HomeServiceImpl implements HomeService
{
@Override
public String wsCall(User user)
{
return "Bravo " + user.getUserName() + " !!";
}
}
Web服务类接口-HelloWS.java
@WebService(name="hello")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWS
{
@WebMethod
public String hello(User user);
}
Web服务实现类-HelloWSImpl.java
@WebService(endpointInterface = "com.springmvcmaven.ws.producer.hello.HelloWS")
public class HelloWSImpl implements HelloWS
{
@Autowired
@Qualifier(value = "homeService")
@Lazy(true)
private HomeService service;
/**
* This is a sample web service operation
* @param user
* @return
*/
@Override
public String hello(User user)
{
return service.wsCall(user);
}
}
web service client call
public class HelloClient
{
public static void main(String[] args)
{
try
{
URL wsdlURL = new URL("http://localhost:8080/SpringMVCMaven/hello?wsdl");
//check above URL in browser, you should see WSDL file
//creating QName using targetNamespace and name
QName qname = new QName("http://hello.producer.ws.springmvcmaven.com/", "HelloWSImplService");
Service service = Service.create(wsdlURL, qname);
//We need to pass interface and model beans to client
HelloWS helloWS = service.getPort(HelloWS.class);
User user = new User();
user.setUserName("JIya");
System.out.println(helloWS.hello(user));
}
catch(Exception e)
{
System.out.println("EXCEPTION => \n" + e);
}
}
}
请指导我,这些代码中缺少的内容。这是正确的方法吗?
谢谢!