Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

May I ask how could I get the following java code work? Is it a servlet or requi

ID: 3908498 • Letter: M

Question

May I ask how could I get the following java code work? Is it a servlet or requiring a tomcat? What java configuration is needed? Thanks!

package com.netease.nim.route;

import com.alibaba.fastjson.JSONObject;

import com.netease.nim.route.CheckSumBuilder;

import org.apache.commons.io.IOUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

@Controller

@RequestMapping(value = {"/route"})

public class RouteController {

public static final Logger logger = LoggerFactory

         .getLogger(RouteController.class);

private final String appSecret = "7bb79g40f44j";

@RequestMapping(value = {"/mockClient.action"}, method = {RequestMethod.POST})

@ResponseBody

public JSONObject mockClient(HttpServletRequest request)

         throws Exception {

      JSONObject result = new JSONObject();

      try {

         byte[] body = readBody(request);

         if (body == null) {

            logger.warn("request wrong, empty body!");

            result.put("code", 414);

            return result;

         }

         String ContentType = request.getContentType();

         String AppKey = request.getHeader("AppKey");

         String CurTime = request.getHeader("CurTime");

         String MD5 = request.getHeader("MD5");

         String CheckSum = request.getHeader("CheckSum");

         logger.info("request headers: ContentType = {}, AppKey = {}, CurTime = {}, " +

                  "MD5 = {}, CheckSum = {}", ContentType, AppKey, CurTime, MD5, CheckSum);

         String requestBody = new String(body, "utf-8");

         logger.info("request body = {}", requestBody);

         String verifyMD5 = CheckSumBuilder.getMD5(requestBody);

         String verifyChecksum = CheckSumBuilder.getCheckSum(appSecret, verifyMD5, CurTime);

         logger.debug("verifyMD5 = {}, verifyChecksum = {}", verifyMD5, verifyChecksum);

         result.put("code", 200);

         return result;

      } catch (Exception ex) {

         logger.error(ex.getMessage(), ex);

         result.put("code", 414);

         return result;

      }

}

private byte[] readBody(HttpServletRequest request) throws IOException {

      if (request.getContentLength() > 0) {

         byte[] body = new byte[request.getContentLength()];

         IOUtils.readFully(request.getInputStream(), body);

         return body;

      } else

         return null;

}

}

Explanation / Answer

@controller annotation on the top of the class tells that you have created a controller this

is the spring framework given annotation, Framework gives you everything, only you need to is to setup and config the env, coding is much easier. The result is that you will know nothing about web dev. However, code with raw API and servlet gives you chance to gain experience and be a programmer.

for the run this program you really need an application server like Tomcat or JBoss server

If you are using annotations based controller then there is no need to extend any class or implement any interface. The only thing you need to do to make your simple java class to become a Spring controller is to add the @Controller annotation to it.

The @RequestMapping annotation is used to map the web request "/route" to the UserController class.

If you are going to use maven project then add the following dependency in pom.xml it will automatically include the all jars related to the spring

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>3.1.0.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.1.0.RELEASE</version>

<exclusions>

<exclusion>

<artifactId>commons-logging</artifactId>

<groupId>commons-logging</groupId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>3.1.0.RELEASE</version>

</dependency>

or you can add jars these jars manually to run this java controller

aopalliance-1.0.jar

commons-logging-1.1.1.jar

spring-webmvc-3.1.2.RELEASE.jar

spring-asm-3.1.2.RELEASE.jar

spring-beans-3.1.2.RELEASE.jar

spring-core-3.1.2.RELEASE.jar

spring-context-3.1.2.RELEASE.jar

spring-aop-3.1.2.RELEASE.jar

spring-expression-3.1.2.RELEASE.jar

spring-context-support-3.1.2.RELEASE.jar

spring-web-3.1.2.RELEASE.jar

then select the class and select run on server