2017-06-29 26 views
1

我正在对使用Spring-MVC编写的web应用程序进行压力测试。为此,我想发送一个对象Person到应用程序。我已经添加了一个系统来获取电子邮件,但每当我发送该对象时,它都是空的。我究竟做错了什么?谢谢。Apache Jmeter:发布一个不适用于ModelAttribute的对象

Server代码:

@RequestMapping(value = "/person/add", method = RequestMethod.POST) 
    public String addPerson(@ModelAttribute("person") Person person, BindingResult bindingResult) { 
     try { 
      ObjectMapper mapper = new ObjectMapper(); 

      String json = mapper.writeValueAsString(person); 
      System.out.println("String is "+json); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 


     System.out.println("Person add called"+person.getUsername()); 
     person.setUsername(this.stripHTML(person.getUsername())); 
     int personId = this.personService.addPerson(person); 
     if (!(personId == 0)) { 
      Person person1 = this.personService.getPersonById(personId); 
      Collection<GrantedAuthority> authorities = new ArrayList<>(); 
      authorities.add(new SimpleGrantedAuthority("ROLE_USER")); 
      Authentication authentication = new UsernamePasswordAuthenticationToken(person1, null, authorities); 
      SecurityContextHolder.getContext().setAuthentication(authentication); 
      return "redirect:/canvaslisting"; 
     } else { 
      return "redirect:/"; 
     } 
    } 

对象身体数据发送:

{"person":{"id":0,"username":"[email protected]","firstName":"test","cleanCacheFlag":false,"googleDrive":false,"dropbox":false,"evernoteConsumed":false,"statusChangeTimeStamp":null,"useCalendar":false,"newsletterFlag":false,"tourSteps":null,"profession":null,"notiz":null,"telePhone":null,"lastVisitedBoards":null,"leftGroup":null,"optionalEmail":null,"facebookLink":null,"xingLink":null,"linkedinLink":null,"lastOnlineTimestamp":null,"userRole":null,"homePage":null,"excelImportQuota":0,"toEmail":null,"code":null,"authorities":null,"role":null,"newpassword":null,"token":null,"profilePhotoString":null,"accountNonExpired":true,"credentialsNonExpired":true,"accountNonLocked":true,"active":true,"enabled":false} 
} 

输出:

Person add callednull 

截图:enter image description here

谢谢。

样品结果

Thread Name: Thread Group 2-5 
Sample Start: 2017-06-29 15:17:11 IST 
Load time: 6 
Connect Time: 0 
Latency: 6 
Size in bytes: 237 
Sent bytes:0 
Headers size in bytes: 205 
Body size in bytes: 32 
Sample Count: 1 
Error Count: 1 
Data type ("text"|"bin"|""): text 
Response code: 415 
Response message: Unsupported Media Type 

Response headers: 
HTTP/1.1 415 Unsupported Media Type 
Server: Apache-Coyote/1.1 
Content-Type: text/html;charset=utf-8 
Content-Language: en 
Content-Length: 1048 
Date: Thu, 29 Jun 2017 09:47:11 GMT 
Connection: close 


HTTPSampleResult fields: 
ContentType: text/html;charset=utf-8 
DataEncoding: utf-8 

请求:

POST http://127.0.0.1:8080/person/add/ 

POST data: 
{"person":{"id":0,"username":"[email protected]","firstName":"test","cleanCacheFlag":false,"googleDrive":false,"dropbox":false,"evernoteConsumed":false,"statusChangeTimeStamp":null,"useCalendar":false,"newsletterFlag":false,"tourSteps":null,"profession":null,"notiz":null,"telePhone":null,"lastVisitedBoards":null,"leftGroup":null,"optionalEmail":null,"facebookLink":null,"xingLink":null,"linkedinLink":null,"lastOnlineTimestamp":null,"userRole":null,"homePage":null,"excelImportQuota":0,"toEmail":null,"code":null,"authorities":null,"role":null,"newpassword":null,"token":null,"profilePhotoString":null,"accountNonExpired":true,"credentialsNonExpired":true,"accountNonLocked":true,"active":true,"enabled":false} 
} 

[no cookies] 

Request Headers: 
Connection: close 
Content-Length: 717 
Content-Type: application/x-www-form-urlencoded 

回答

1

更改HTTP标题管理器中的标头内容类型:

Content-Type: application/x-www-form-urlencoded 

要:

Content-Type: application/json 
+0

你想在哪里添加这个?在Jmeter中,在http头管理器中? –

+0

这个工作的人..谢谢...在Jmeter中添加它.. –

1

你不应该只是对人的@RequestBody注解?

public String addPerson(@RequestBody Person person); 
+0

仍然没有运气。 HTTP/1.1 415不支持的媒体类型 –

0

嗯,在你的JMeter请求一个错字或复制粘贴问题

JMeter request issue

而且你的JMeter的配置可能会丢失HTTP Header Manager配置为发送Content-Type头与application/json

+0

尝试过的人,人,p。他们全部 –